a6b1fd4985 2011-09-28 kinaba: #include <iostream> a6b1fd4985 2011-09-28 kinaba: #include <sstream> a6b1fd4985 2011-09-28 kinaba: #include <iomanip> a6b1fd4985 2011-09-28 kinaba: #include <vector> a6b1fd4985 2011-09-28 kinaba: #include <string> a6b1fd4985 2011-09-28 kinaba: #include <map> a6b1fd4985 2011-09-28 kinaba: #include <set> a6b1fd4985 2011-09-28 kinaba: #include <algorithm> a6b1fd4985 2011-09-28 kinaba: #include <numeric> a6b1fd4985 2011-09-28 kinaba: #include <iterator> a6b1fd4985 2011-09-28 kinaba: #include <functional> a6b1fd4985 2011-09-28 kinaba: #include <complex> a6b1fd4985 2011-09-28 kinaba: #include <queue> a6b1fd4985 2011-09-28 kinaba: #include <valarray> a6b1fd4985 2011-09-28 kinaba: #include <stack> a6b1fd4985 2011-09-28 kinaba: #include <cmath> a6b1fd4985 2011-09-28 kinaba: #include <cassert> a6b1fd4985 2011-09-28 kinaba: #include <cstring> a6b1fd4985 2011-09-28 kinaba: using namespace std; a6b1fd4985 2011-09-28 kinaba: typedef long long LL; a6b1fd4985 2011-09-28 kinaba: typedef complex<double> CMP; a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: static const int MODVAL = 1000000007; a6b1fd4985 2011-09-28 kinaba: struct mint a6b1fd4985 2011-09-28 kinaba: { a6b1fd4985 2011-09-28 kinaba: int val; a6b1fd4985 2011-09-28 kinaba: mint():val(0){} a6b1fd4985 2011-09-28 kinaba: mint(int x):val(x%MODVAL) {} a6b1fd4985 2011-09-28 kinaba: mint(size_t x):val(x%MODVAL) {} a6b1fd4985 2011-09-28 kinaba: mint(LL x):val(x%MODVAL) {} a6b1fd4985 2011-09-28 kinaba: }; a6b1fd4985 2011-09-28 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } a6b1fd4985 2011-09-28 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } a6b1fd4985 2011-09-28 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } a6b1fd4985 2011-09-28 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } a6b1fd4985 2011-09-28 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } a6b1fd4985 2011-09-28 kinaba: mint operator+(mint x, mint y) { return x+=y; } a6b1fd4985 2011-09-28 kinaba: mint operator-(mint x, mint y) { return x-=y; } a6b1fd4985 2011-09-28 kinaba: mint operator*(mint x, mint y) { return x*=y; } a6b1fd4985 2011-09-28 kinaba: mint operator/(mint x, mint y) { return x/=y; } a6b1fd4985 2011-09-28 kinaba: mint inv2 = mint(1) / 2; a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: class Nim { public: a6b1fd4985 2011-09-28 kinaba: int count(int K, int L) a6b1fd4985 2011-09-28 kinaba: { a6b1fd4985 2011-09-28 kinaba: valarray<mint> v(65536); a6b1fd4985 2011-09-28 kinaba: v[slice(2,L-1,1)] = 1; a6b1fd4985 2011-09-28 kinaba: for(unsigned p=2; p<=L; ++p) a6b1fd4985 2011-09-28 kinaba: if( v[p].val ) a6b1fd4985 2011-09-28 kinaba: for(unsigned q=p*p; q<=L; q+=p) a6b1fd4985 2011-09-28 kinaba: v[q] = 0; a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: pre(v, 0, 65536); a6b1fd4985 2011-09-28 kinaba: valarray<mint> a(1, 65536); a6b1fd4985 2011-09-28 kinaba: for(int i=1; i<=K; i<<=1, v*=v) a6b1fd4985 2011-09-28 kinaba: if( K & i ) a6b1fd4985 2011-09-28 kinaba: a *= v; a6b1fd4985 2011-09-28 kinaba: post(a, 0, 65536); a6b1fd4985 2011-09-28 kinaba: return a[0].val; a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: void pre(valarray<mint>& v, int s, int e) a6b1fd4985 2011-09-28 kinaba: { a6b1fd4985 2011-09-28 kinaba: if( s+1 == e ) a6b1fd4985 2011-09-28 kinaba: return; a6b1fd4985 2011-09-28 kinaba: int m = (s+e)/2; a6b1fd4985 2011-09-28 kinaba: pre(v, s, m); a6b1fd4985 2011-09-28 kinaba: pre(v, m, e); a6b1fd4985 2011-09-28 kinaba: for(int i=s,j=m; i<m; ++i,++j) { a6b1fd4985 2011-09-28 kinaba: mint vi=v[i], vj=v[j]; a6b1fd4985 2011-09-28 kinaba: v[i] = vi - vj; a6b1fd4985 2011-09-28 kinaba: v[j] = vi + vj; a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: void post(valarray<mint>& v, int s, int e) a6b1fd4985 2011-09-28 kinaba: { a6b1fd4985 2011-09-28 kinaba: if( s+1 == e ) a6b1fd4985 2011-09-28 kinaba: return; a6b1fd4985 2011-09-28 kinaba: int m = (s+e)/2; a6b1fd4985 2011-09-28 kinaba: for(int i=s,j=m; i<m; ++i,++j) { a6b1fd4985 2011-09-28 kinaba: mint dif=v[i], sum=v[j]; a6b1fd4985 2011-09-28 kinaba: v[i] = (sum + dif) * inv2; a6b1fd4985 2011-09-28 kinaba: v[j] = (sum - dif) * inv2; a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: post(v, s, m); a6b1fd4985 2011-09-28 kinaba: post(v, m, e); a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: }; a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: // BEGIN CUT HERE a6b1fd4985 2011-09-28 kinaba: #include <ctime> a6b1fd4985 2011-09-28 kinaba: double start_time; string timer() a6b1fd4985 2011-09-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } a6b1fd4985 2011-09-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) a6b1fd4985 2011-09-28 kinaba: { os << "{ "; a6b1fd4985 2011-09-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) a6b1fd4985 2011-09-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } a6b1fd4985 2011-09-28 kinaba: void verify_case(const int& Expected, const int& Received) { a6b1fd4985 2011-09-28 kinaba: bool ok = (Expected == Received); a6b1fd4985 2011-09-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; a6b1fd4985 2011-09-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } a6b1fd4985 2011-09-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); a6b1fd4985 2011-09-28 kinaba: #define END verify_case(_, Nim().count(K, L));} a6b1fd4985 2011-09-28 kinaba: int main(){ a6b1fd4985 2011-09-28 kinaba: a6b1fd4985 2011-09-28 kinaba: CASE(0) a6b1fd4985 2011-09-28 kinaba: int K = 3; a6b1fd4985 2011-09-28 kinaba: int L = 7; a6b1fd4985 2011-09-28 kinaba: int _ = 6; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: CASE(1) a6b1fd4985 2011-09-28 kinaba: int K = 4; a6b1fd4985 2011-09-28 kinaba: int L = 13; a6b1fd4985 2011-09-28 kinaba: int _ = 120; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: CASE(2) a6b1fd4985 2011-09-28 kinaba: int K = 10; a6b1fd4985 2011-09-28 kinaba: int L = 100; a6b1fd4985 2011-09-28 kinaba: int _ = 294844622; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: CASE(3) a6b1fd4985 2011-09-28 kinaba: int K = 123456789; a6b1fd4985 2011-09-28 kinaba: int L = 12345; a6b1fd4985 2011-09-28 kinaba: int _ = 235511047; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: CASE(4) a6b1fd4985 2011-09-28 kinaba: int K = 1000000000; a6b1fd4985 2011-09-28 kinaba: int L = 50000; a6b1fd4985 2011-09-28 kinaba: int _ = 428193537; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: /* a6b1fd4985 2011-09-28 kinaba: CASE(5) a6b1fd4985 2011-09-28 kinaba: int K = ; a6b1fd4985 2011-09-28 kinaba: int L = ; a6b1fd4985 2011-09-28 kinaba: int _ = ; a6b1fd4985 2011-09-28 kinaba: END a6b1fd4985 2011-09-28 kinaba: */ a6b1fd4985 2011-09-28 kinaba: } a6b1fd4985 2011-09-28 kinaba: // END CUT HERE