ADDED SRM/546/1A.cpp Index: SRM/546/1A.cpp ================================================================== --- SRM/546/1A.cpp +++ SRM/546/1A.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class KleofasTail { public: + long long countGoodSequences(long long K, long long A, long long B) + { + return solve(K,B) - solve(K,A-1); + } + LL solve(LL K, LL U) + { + if(U==-1) + return 0; + if(K==0) + return U+1; + + LL v = 0; + for(int i=0; (K< +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 long long& Expected, const long long& 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(_, KleofasTail().countGoodSequences(K, A, B));} +int main(){ +CASE(0) + long long K = 3LL; + long long A = 4LL; + long long B = 8LL; + long long _ = 2LL; +END +CASE(1) + long long K = 1LL; + long long A = 23457LL; + long long B = 123456LL; + long long _ = 100000LL; +END +CASE(2) + long long K = 1234567890123456LL; + long long A = 10LL; + long long B = 1000000LL; + long long _ = 0LL; +END +CASE(3) + long long K = 0LL; + long long A = 0LL; + long long B = 2LL; + long long _ = 3LL; +END +CASE(4) + long long K = 2LL; + long long A = 3LL; + long long B = 3LL; + long long _ = 1LL; +END +CASE(5) + long long K = 13LL; + long long A = 12345LL; + long long B = 67890123LL; + long long _ = 8387584LL; +END +CASE(6) + long long K = 2LL; + long long A = 0LL; + long long B = 6LL; + long long _ = 5LL; +END +CASE(7) + long long K = 2LL; + long long A = 0LL; + long long B = 9999999999999LL; + long long _ = -1LL; +END + +} +// END CUT HERE ADDED SRM/546/1B.cpp Index: SRM/546/1B.cpp ================================================================== --- SRM/546/1B.cpp +++ SRM/546/1B.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class FavouriteDigits { public: + long long findNext(long long N, int digit1, int count1, int digit2, int count2) + { + LL d = 1; + for(int _=0; _<15; ++_) d *= 10; + + LL answer = 0; + + bool zeros = true; + for(int rest=15; d>=1; d/=10, --rest) { + for(int v=0; v<=9; ++v) { + int c1 = max(0, count1 - (v==digit1 && (v!=0 || !zeros) ? 1 : 0)); + int c2 = max(0, count2 - (v==digit2 && (v!=0 || !zeros) ? 1 : 0)); + bool zz = zeros && (v==0); + if( rest >= c1 + c2 ) { + LL cur = answer + v*d + largest(rest, digit1, c1, digit2, c2); + if( cur >= N ) { + answer += v*d; + count1 = c1; + count2 = c2; + zeros = zz; + break; + } + } + } + } + + return answer; + } + + LL largest(int rest, int d1, int c1, int d2, int c2) + { + if(rest==0) + return 0; + if(d1>d2) swap(d1,d2), swap(c1,c2); + string s; + for(int i=0; i> n; + return n; + } +}; + +// BEGIN CUT HERE +#include +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 long long& Expected, const long long& 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(_, FavouriteDigits().findNext(N, digit1, count1, digit2, count2));} +int main(){ + +CASE(0) + long long N = 47LL; + int digit1 = 1; + int count1 = 0; + int digit2 = 2; + int count2 = 0; + long long _ = 47LL; +END +CASE(1) + long long N = 47LL; + int digit1 = 5; + int count1 = 0; + int digit2 = 9; + int count2 = 1; + long long _ = 49LL; +END +CASE(2) + long long N = 47LL; + int digit1 = 5; + int count1 = 0; + int digit2 = 3; + int count2 = 1; + long long _ = 53LL; +END +CASE(3) + long long N = 47LL; + int digit1 = 2; + int count1 = 1; + int digit2 = 0; + int count2 = 2; + long long _ = 200LL; +END +CASE(4) + long long N = 123456789012345LL; + int digit1 = 1; + int count1 = 2; + int digit2 = 2; + int count2 = 4; + long long _ = 123456789012422LL; +END +CASE(5) + long long N = 92LL; + int digit1 = 1; + int count1 = 1; + int digit2 = 0; + int count2 = 0; + long long _ = 100LL; +END +/* +CASE(6) + long long N = LL; + int digit1 = ; + int count1 = ; + int digit2 = ; + int count2 = ; + long long _ = LL; +END +*/ +CASE(7) + long long N = 1000000000000000LL - 1; + int digit1 = 5; + int count1 = 5; + int digit2 = 5; + int count2 = 5; + long long _ = -1LL; +END + +} +// END CUT HERE ADDED SRM/546/1C.cpp Index: SRM/546/1C.cpp ================================================================== --- SRM/546/1C.cpp +++ SRM/546/1C.cpp @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +static const int MODVAL = 1000000009; +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; } +vector FAC_(1,1); +mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } +vector< vector > CP_; // Pascal's triangle: if O(1) nCk is needed. +mint C(LL n, LL k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector(nn+1,1)); + for(int kk=1; kk afterFourClicks) + { + stringstream sin(accumulate(afterFourClicks.begin(), afterFourClicks.end(), string(""))); + vector v; + for(int n; sin>>n; ) + v.push_back(n); + return solve(v, v.size()); + } + + int solve(const vector& S, int N) + { + vector cycles; + { + vector used(N, false); + for(int s=0; s& cycles) + { + map cc; + for(int i=0; i::iterator it=cc.begin(); it!=cc.end(); ++it) + { + int clen = it->first; + int num = it->second; + + mint my = 0; + if(clen%2 == 0) + { + // 4n -> n*4 + if( num%4 != 0 ) + return 0; + my = split<4>(num) * POW(POW(clen,3)*3*2*1, num/4); + } + else + { + // o -> o * 1 + // 2o -> o * 2 + // 4o -> o * 4 + + for(int fnum=0; fnum<=num; fnum+=4) + for(int tnum=0; fnum+tnum<=num; tnum+=2) { + int onum=num-fnum-tnum; + my += split<4>(fnum)*POW(POW(clen,3)*3*2*1, fnum/4) + *split<2>(tnum)*POW(POW(clen,1)*1, tnum/2) + *C(num,fnum)*C(num-fnum,tnum); + } + } + total *= my; + } + return total.val; + } + + template + mint split(int n) + { + static vector memo(10000); + static vector used(10000); + if( n<=k ) + return 1; + else { + if(used[n]) + return memo[n]; + used[n] = true; + return memo[n] = C(n-1,k-1) * split(n-k); + } + } +}; + +// BEGIN CUT HERE +#include +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(_, FleaCircus().countArrangements(afterFourClicks));} +int main(){ + +CASE(0) + string afterFourClicks_[] = {"1 2 0 3"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 1; +END +CASE(1) + string afterFourClicks_[] = {"1 2 ", "0 3"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 1; +END +CASE(2) + string afterFourClicks_[] = {"0 1 2"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 4; +END +CASE(3) + string afterFourClicks_[] = {"0 1 2 3 5 4"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 0; +END +CASE(4) + string afterFourClicks_[] = {"3 6 7 9 8 2 1", "0 5 1 0 4"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 4; +END +CASE(5) + string afterFourClicks_[] = {"1 0 7 5 6 3 4 2"}; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = 48; +END +/* +CASE(6) + string afterFourClicks_[] = ; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = ; +END +CASE(7) + string afterFourClicks_[] = ; + vector afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); + int _ = ; +END +*/ +} +// END CUT HERE