ADDED SRM/518-U/1C.cpp Index: SRM/518-U/1C.cpp ================================================================== --- SRM/518-U/1C.cpp +++ SRM/518-U/1C.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const int MODVAL = 1000000007; +struct mint +{ + int val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} + mint(size_t x):val(x%MODVAL) {} + mint(LL x):val(x%MODVAL) {} +}; +mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } +mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } +mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } +mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } +mint operator+(mint x, mint y) { return x+=y; } +mint operator-(mint x, mint y) { return x-=y; } +mint operator*(mint x, mint y) { return x*=y; } +mint operator/(mint x, mint y) { return x/=y; } +mint inv2 = mint(1) / 2; + +class Nim { public: + int count(int K, int L) + { + valarray v(65536); + v[slice(2,L-1,1)] = 1; + for(unsigned p=2; p<=L; ++p) + if( v[p].val ) + for(unsigned q=p*p; q<=L; q+=p) + v[q] = 0; + + pre(v, 0, 65536); + valarray a(1, 65536); + for(int i=1; i<=K; i<<=1, v*=v) + if( K & i ) + a *= v; + post(a, 0, 65536); + return a[0].val; + } + + void pre(valarray& v, int s, int e) + { + if( s+1 == e ) + return; + int m = (s+e)/2; + pre(v, s, m); + pre(v, m, e); + for(int i=s,j=m; i& v, int s, int e) + { + if( s+1 == e ) + return; + int m = (s+e)/2; + for(int i=s,j=m; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const int& Expected, const int& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, Nim().count(K, L));} +int main(){ + +CASE(0) + int K = 3; + int L = 7; + int _ = 6; +END +CASE(1) + int K = 4; + int L = 13; + int _ = 120; +END +CASE(2) + int K = 10; + int L = 100; + int _ = 294844622; +END +CASE(3) + int K = 123456789; + int L = 12345; + int _ = 235511047; +END +CASE(4) + int K = 1000000000; + int L = 50000; + int _ = 428193537; +END +/* +CASE(5) + int K = ; + int L = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED lib/doc/fft.txt Index: lib/doc/fft.txt ================================================================== --- lib/doc/fft.txt +++ lib/doc/fft.txt @@ -0,0 +1,63 @@ + +http://apps.topcoder.com/wiki/display/tc/SRM+518 + + +以下のような関数を考える。 + +int[] xor_count(int[] a, int[] b, int N) +{ + int[] c = {0} * N; + for(int i=0; i {x} +N=2 の場合、pre : {x,y} --> {x-y, x+y} + xor_count( {x,y}, {u,v} ) = {xu+yv, yu+xv} + と {x-y, x+y} * {u-v, u+v} = {xu+yv-xu-xv, xu+yv+yu+xv} + から確認できる。 +一般にNが2のベキの場合、xorの作用するビットのパターンが再帰的なので + pre( [X,Y] ) = [pre(X)-pre(Y), pre(X)+pre(Y)] +と再帰的にすれば成り立つ + + + + + + +xorでなくて足し算の場合 + +int[] plus_count(int[] a, int[] b, int N) +{ + int[] c = {0} * 2N; + for(int i=0; i