import proj_euler as pe import numpy as np mod = 10**9+7 max_n = 10**4+10 fact, ifact = pe.mod_fact_cache(mod, max_n = max_n) def comb(n,k): if k > n: return 0 if n < mod: return (((int(fact[n]) * int(ifact[k])) % mod )* int(ifact[n-k])) % mod n_digits, k_digits = pe.base_repr(n, mod), pe.base_repr(k, mod) res = 1 for top, bot in zip(n_digits, k_digits): res = (res * C(top, bot)) % mod return res def double_power_set(n): #for prime mods, the totient is p-1 return pow(2, pow(2, n, mod-1) -1 ,mod) def C(n,k): #build cache tables cache = np.zeros((n+1, k+1), dtype=int) exact = np.zeros(n+1, dtype=int) for j in xrange(1, n+1): if j%100 == 0: print "on "+str(j) #all possible graphs cache[j,1] = cache[j-1,1] + double_power_set(j) - double_power_set(j-1)
import proj_euler as pe import numpy as np #n = 5*10**4 mod = 1000000123 fact, ifact = pe.mod_fact_cache(mod, max_n = 6*10**4+2) def P(k, r, n): if k == n: return 1 q, rem = divmod(n-1,k) rem += 1 dividers = q divs = np.zeros(dividers+2,np.int64) #number of pieces available divs[0] = 1 divs[1] = pow(int(ifact[k]), r, mod) pows = np.array([(-1) ** (j%2) * pow(int(ifact[j*k]), r, mod) for j in xrange(dividers+1)]) for i in xrange(2, dividers+1): temp = np.remainder(pows[1:i+1] * divs[i-1::-1], mod) divs[i] -= np.sum(temp) divs[i] %= mod for j in xrange(1, len(divs)): # print divs[-j-1], j divs[-1] -= (-1) ** (j%2) * pow(int(ifact[(j-1)*k+rem]), r, mod) * divs[-j-1] divs[-1] %= mod return (divs[-1] * pow(int(fact[n]), r, mod)) % mod def Q(n): tot = 0 for k in xrange(1,n+1): if k < 10: print "On %d" %k