\(K\)é¢ãã¤ã¹(ãµã¤ã³ã)ã\(N\)åæããæã«ãåè¨ãããå¤ã«ãªã確çãæ±ããã
åè¨ãããå¤ã«ãªãå ´åã®æ°ãæ±ãã¦\(K^N\)ã§å²ã£ã¦ç¢ºçã«ããæ¹ããããããããªã®ã§ãå ´åã®æ°ãæ±ããæ¹æ³ãèãã
ç·å½ã
\(N\)åã®ãã¤ã¹ã«ã¤ãã¦ãããã\(1\)ãã\(K\)ã¾ã§ã«ã¼ããããã¤ã¼ããªç·å½ãã§åãæ±ãã¦ããã¨\(O(K^N)\)ãããã®è¨ç®éãå¿ è¦ã«ãªã£ã¦ãã¾ãããµã¤ã³ãã®æ°ãå¢ããã¨ç¾å®çãªæéã§çµãããªããªã£ã¦ãã¾ã
åçè¨ç»æ³
漸åå¼ãç«ã¦ã¦åçè¨ç»æ³ã§è¨ç®ããã¨\(O(N^2K^2)\)ãããã§è¨ç®ã§ãã
å
·ä½çã«ã¯\(n\)åãµã¤ã³ãã使ã£ã¦åè¨ã\(c\)ã«ãªãå ´åã®æ°ãæ±ãããã¨ã\(f_n(c) = \sum_{i=1}^K f_{n-1}(c-i)\)ã¨ãã漸åå¼ãæãç«ã¤
K = 6 N = 100 dp = [[0] * N * K for i in xrange(2)] for i in xrange(K): dp[0][i] = 1 for i in xrange(1, N): for j in xrange(N * K): dp[i % 2][j] = 0 for k in xrange(max(0, j - K), j): dp[i % 2][j] += dp[(i - 1) % 2][k] for j in dp[N - 1 % 2]: print j, print
åçè¨ç»æ³+ç´¯ç©å
åçè¨ç»æ³ã®ä¸ã¤ã®ãµã¤ã³ãã«å¯¾ããã«ã¼ãã§ãæ·»ãåã«\(1\)ãã\(K\)足ããç¯å²ã«åãæ°ã足ãã¦ããã®ã§ãç´¯ç©åã«é¢ããã¢ã«ã´ãªãºã ã使ãã°ããé«éåã§ãã
\(0\)åæåããé
åãç¨æãã¦ã足ãããç¯å²ã®éå§å°ç¹ã«è¶³ãããæ°ãå ãã¦ç¯å²ã®çµäºå°ç¹ã®æ¬¡ã®æ·»åã®å ´æã«è¶³ãããæ°ãå¼ããé
åãä½ã£ã¦ãåå°ç¹ã¾ã§ã®è¦ç´ ã®ç´¯ç©åã®é
åãè¨ç®ããã°ãã
ä¸è¨è¨äºã®å³ããããããã
è¨ç®éã¯\(O(N^2K)\)ã«ãªã
K = 6 N = 100 dp = [[0] * N * K for i in xrange(2)] for i in xrange(K): dp[0][i] = 1 for i in xrange(1, N): dp[i % 2] = [0] * N * K for j in xrange(N * K - 1): dp[i % 2][j + 1] += dp[(i - 1) % 2][j] if j + K + 1 < N * K: dp[i % 2][j + K + 1] -= dp[(i - 1) % 2][j] cur = 0 for j in xrange(N * K): cur += dp[i % 2][j] dp[i % 2][j] = cur for j in dp[N - 1 % 2]: print j, print
ãã¼ãªã¨å¤æ
確çå¤æ°ã®åã®åå¸ã¯å
ã®åå¸ã®ç³ã¿è¾¼ã¿ã«ãªãã®ã§ããã¼ãªã¨å¤æãã¦ä¿æ°ããããããã¦éãã¼ãªã¨å¤æããã¨ç®çã®å¤ãå¾ããã(ã¯ã)
è¨ç®éã¯\(O(NK \log NK)\)
ãã ããµã¤ã³ãã®æ°ãå¢ãããããªã¼ãã¼ããã¼ãããå¤ãªå¤ã«ãªã£ãããã¦æ£ããçããå¾ãããªãã£ã(´ã»Ïã»ï½)
æµ®åå°æ°ç¹æ¼ç®ã®èª¤å·®ãªã®ããªãâ¦â¦(ï¼)
æ¤è¨¼ãã¦ãã ãã£ãæ¹ãããã®ã§è¿½è¨(2015-09-09)
@Scaled_Wurm (ΦÏΦ)ï¼ãã®ãµã¤ã³ãã®FFTã®ãã¤ããã£ã±ç²¾åº¦ã®åé¡ã§ãmpmathã ã£ããããã£ã½ãçããåºãã§ãã http://t.co/0zbfflHgTQ
— ininsanus (@ininsanus) 2015å¹´9æ9æ¥
ã½ã¼ã¹âhttps://t.co/Z6qN6ebr9t
# -*- coding: utf-8 -*- import math,string,itertools,fractions,heapq,collections,re,array,bisect #http://www.slideshare.net/chokudai/fft-49066791 import cmath def pow_2_at_least(x): n = 1 while n < x: n *= 2 return n def convolution(g, N): Lg = len(g) n = pow_2_at_least(Lg * N) g = g + [0] * (n - Lg) zeta_arr = [cmath.exp(2j * cmath.pi / n) for n in xrange(1, n + 1)] gf = fft(g, n, zeta_arr) ff = [pow(gf[i], N) for i in xrange(n)] return ifft(ff, n, zeta_arr) def _fft(f, n, zeta_arr, inv): m = n / 2 if m == 1: f0 = [f[0]] f1 = [f[1]] else: f0 = [f[2 * i + 0] for i in xrange(m)] f1 = [f[2 * i + 1] for i in xrange(m)] f0 = _fft(f0, m, zeta_arr, inv) f1 = _fft(f1, m, zeta_arr, inv) sign = 1 - 2 * inv if inv: zeta = 1.0 / zeta_arr[n - 1] else: zeta = zeta_arr[n - 1] pow_zeta = 1 for i in xrange(n): f[i] = f0[i % m] + pow_zeta * f1[i % m] pow_zeta *= zeta return f def fft(f, n, zeta_arr): return _fft(f, n, zeta_arr, False) def ifft(f, n, zeta_arr): return [i / n for i in _fft(f, n, zeta_arr, True)] K = 6 N = 100 print '\n'.join([str(int(round(i.real))) for i in convolution([0] + [1.0] * K, N)][1:N * K + 1])
åèURL
ãããããªç¢ºçã¨ããè¼ã£ã¦ã
\(K\)é¢ãã¤ã¹ã\(N\)åæããæã«åè¨ãããå¤\(C\)ã«ãªã確çãæ±ãã以ä¸ã®å¼ã®å°åºãè¼ã£ã¦ãã
$$P(N, K, C)=\frac{1}{K^N}\sum_{i=0}^{\lfloor \frac{C - N}{K} \rfloor}(-1)^i \binom Ni \binom{C-si-1}{N-1}$$