ADDED SRM/TCO11-3/1A.cpp Index: SRM/TCO11-3/1A.cpp ================================================================== --- SRM/TCO11-3/1A.cpp +++ SRM/TCO11-3/1A.cpp @@ -0,0 +1,120 @@ +#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; + +int gcd(int x, int y) +{ + while(x) + swap(x, y%=x); + return y; +} + +int lcm(int x, int y) +{ + return x/gcd(x,y)*y; +} + +class ArtShift { public: + int maxShifts(string sequence) + { + int B=0, W=0; + for(int i=0; i()); + for(int b=0; b<=B; ++b) memo[b*31+0].insert(1); + for(int w=0; w<=W; ++w) memo[0*31+w].insert(1); + return *rec(B,W).rbegin(); + } + + vector< set > memo; + const set& rec(int B, int W) + { + set& result = memo[B*31+W]; + if( !result.empty() ) + return result; + + result.insert(B+W); + for(int b= 0; b<=B; ++b) + for(int w=!b; w<=W; ++w) { + const set& x = rec(b,w); + const set& y = rec(B-b,W-w); + for(set::const_iterator it=x.begin(); it!=x.end(); ++it) + for(set::const_iterator jt=y.begin(); jt!=y.end(); ++jt) + result.insert(lcm(*it,*jt)); + } + return result; + } +}; + +// 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(_, ArtShift().maxShifts(sequence));} +int main(){ + +CASE(0) + string sequence = "BW"; + int _ = 2; +END +CASE(1) + string sequence = "BBBWBBB"; + int _ = 7; +END +CASE(2) + string sequence = "BWBWB"; + int _ = 6; +END +CASE(3) + string sequence = "WWWWWWWWW"; + int _ = 1; +END +CASE(4) + string sequence = "WWWWWWWWWBBWB"; + int _ = 60; +END +CASE(5) + string sequence = "W"; + int _ = 1; +END +CASE(6) + string sequence = "B"; + int _ = 1; +END +CASE(7) + string sequence = "BB"; + int _ = 1; +END +CASE(8) + string sequence = "BWBWBWBWBWBWBWBWBWBWBWBWBWBWBW"; + int _ = -1; +END + +} +// END CUT HERE ADDED SRM/TCO11-3/1B.cpp Index: SRM/TCO11-3/1B.cpp ================================================================== --- SRM/TCO11-3/1B.cpp +++ SRM/TCO11-3/1B.cpp @@ -0,0 +1,158 @@ +#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; + +class PrefixFreeSuperset { public: + long long minSumLength(vector cur, long long k) + { + map open; { + string me(""); + rec(me, cur, open); + } + + LL totalLen = 0; + for(int i=0; i::iterator it = open.begin(); + int lll = it->first; + LL ccc = it->second; + open.erase(it); + + if( k <= ccc ) { + totalLen += lll*k; + k = 0; + } + else if( k <= ccc + open[lll+1] ) { + totalLen += lll*ccc; + k -= ccc; + } + else if( k <= ccc*2 + open[lll+1] ) { + LL div = k - (ccc+open[lll+1]); + open[lll+1] += div*2; + totalLen += (ccc-div)*lll; + k -= (ccc-div); + } + else + open[lll+1] += ccc*2; + } + return totalLen; + } + + void rec(string& me, const vector& prefix, map& open) + { + bool iamprefix = false; + for(int i=0; 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 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(_, PrefixFreeSuperset().minSumLength(cur, k));} +int main(){ + +CASE(0) + string cur_[] = {"010"}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 4LL; + long long _ = 9LL; +END +CASE(1) + string cur_[] = {"01","000"}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 4LL; + long long _ = 9LL; +END +CASE(2) + string cur_[] = {"0011","011110101","11101010111","11101010100000000","11101010100000001111"}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 1000000000000LL; + long long _ = 39971901640560LL; +END +CASE(3) + string cur_[] = {"010","00","011","1"}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 4LL; + long long _ = 9LL; +END +CASE(4) + string cur_[] = {"010","00","011","1"}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 5LL; + long long _ = -1LL; +END +CASE(5) + string cur_[] = {"0011010", +"0011011110", +"00110111001", +"0011011111", +"00110111000", +}; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = 9LL; + long long _ = 58LL; +END +/* +CASE(6) + string cur_[] = ; + vector cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_)); + long long k = LL; + long long _ = LL; +END +*/ +} +// END CUT HERE ADDED SRM/TCO11-3/1C.cpp Index: SRM/TCO11-3/1C.cpp ================================================================== --- SRM/TCO11-3/1C.cpp +++ SRM/TCO11-3/1C.cpp @@ -0,0 +1,118 @@ +#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; + +template +struct DP3x +{ + int N1, N2, N3; + vector data; + DP3x(int, int N2, int N3, const T& t = T()) + : N1(2), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T) < (1<<26)); } + T& operator()(int i1, int i2, int i3) + { i1&=1; return data[ ((i1*N2)+i2)*N3+i3 ]; } +}; + +static const int MODVAL = 1000000007; + +class RowOfColors { public: + int countWays(int w, int h, int k) + { + DP3x dp(w+1, h+1, k+1); + for(int i=w; i>=0; --i) + for(int stack=0; stack<=h; ++stack) + for(int used =0; used <=k; ++used) + if( i == w ) + dp(i,stack,used) = (stack==1 && used==k ? 1 : 0); + else { + LL cnt = 0; + if(stack+1<=h && used+1<=k) // push new color + cnt += dp(i+1,stack+1,used+1); + if(stack) // continue the same color + cnt += dp(i+1,stack,used); + if(stack && used+1<=k) // replace top + cnt += dp(i+1,stack,used+1); + if(stack>=2) // pop + cnt += dp(i+1,stack-1,used); + dp(i,stack,used) = cnt % MODVAL; + } + LL v = dp(0,0,0); + for(int i=1; i<=k; ++i) + v = (v*i) % MODVAL; + return int(v); + } +}; + +// 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(_, RowOfColors().countWays(w, h, k));} +int main(){ + +CASE(0) + int w = 4; + int h = 1; + int k = 2; + int _ = 6; +END +CASE(1) + int w = 4; + int h = 3; + int k = 2; + int _ = 12; +END +CASE(2) + int w = 4; + int h = 4; + int k = 10; + int _ = 0; +END +CASE(3) + int w = 14; + int h = 28; + int k = 14; + int _ = 178290591; +END +/* +CASE(4) + int w = ; + int h = ; + int k = ; + int _ = ; +END +CASE(5) + int w = ; + int h = ; + int k = ; + int _ = ; +END +*/ +} +// END CUT HERE