DELETED SRM/550-U/1A.cpp Index: SRM/550-U/1A.cpp ================================================================== --- SRM/550-U/1A.cpp +++ SRM/550-U/1A.cpp @@ -1,186 +0,0 @@ -#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 RotatingBot { public: - int minArea(vector moves) - { - if( moves.size() == 1 ) { - int w = moves[0]+1; - return w; - } - if( moves.size() == 2 ) { - int w = moves[0]+1; - int h = moves[1]+1; - return w*h; - } - if( moves.size() == 3 ) { - int w1 = moves[0]+1; - int h = moves[1]+1; - int w2 = moves[2]+1; - return max(w1,w2)*h; - } - if( moves.size() == 4 ) { - int w1 = moves[0]+1; - int h1 = moves[1]+1; - int w2 = moves[2]+1; - int h2 = moves[3]+1; - if( w1 > w2 ) - return -1; - if( w1 == w2 ) { - if( h1 <= h2 ) - return -1; - else - return w1*h1; - } - return w2*max(h1,h2); - } - - int w1 = moves[0]+1; - int h1 = moves[1]+1; - int w2 = moves[2]+1; - int h2 = moves[3]+1; - if( w1 > w2 ) - return -1; - if( w1 == w2 ) { - vector rm = simulate(w1, h1, 0, 0); - if( match(rm, moves) ) - return w1*h1; - else - return -1; - } else { - if( h2

rm = simulate(w2, h2, w2-w1, h2-h1); - if( match(rm, moves) ) - return w2*h2; - else - return -1; - } - } - - vector simulate(int w, int h, int x, int y) - { - vector moves; - vector< vector > vis(h, vector(w)); - int dx[]={+1,0,-1,0}; - int dy[]={0,+1,0,-1}; - int d = 0; - vis[y][x] = true; - for(;;) { - int cnt = 0; - for(;;) { - int xx=x+dx[d]; - int yy=y+dy[d]; - if( yy<0||h<=yy||xx<0||w<=xx||vis[yy][xx] ) - break; - y=yy, x=xx; - vis[y][x] = true; - ++cnt; - } - d = (d+1)%4; - if( cnt == 0 ) - break; - moves.push_back(cnt); - } - return moves; - } - - bool match(const vector& rm, const vector& m) - { - if(rm.size() < m.size()) - return false; - for(int i=0; i+1= m[m.size()-1]; - } -}; - -// 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(_, RotatingBot().minArea(moves));} -int main(){ - -CASE(0) - int moves_[] = {15}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 16; -END -CASE(1) - int moves_[] = {3,10}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 44; -END -CASE(2) - int moves_[] = {1,1,1,1}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = -1; -END -CASE(3) - int moves_[] = {9,5,11,10,11,4,10}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 132; -END -CASE(4) - int moves_[] = {12,1,27,14,27,12,26,11,25,10,24,9,23,8,22,7,21,6,20,5,19,4,18,3,17,2,16,1,15}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 420; -END -CASE(5) - int moves_[] = {8,6,6,1}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = -1; -END -CASE(6) - int moves_[] = {8,6,6}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 63; -END -CASE(7) - int moves_[] = {5,4,5,3,3}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 30; -END -CASE(8) -int moves_[] = {2,2,2,1}; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = 9; -END -/* -CASE(9) - int moves_[] = ; - vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); - int _ = ; -END -*/ -} -// END CUT HERE DELETED SRM/550-U/1B.cpp Index: SRM/550-U/1B.cpp ================================================================== --- SRM/550-U/1B.cpp +++ SRM/550-U/1B.cpp @@ -1,126 +0,0 @@ -#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 CheckerExpansion { public: - vector resultAfter(long long t, long long x0, long long y0, int w, int h) - { - vector answer(h, string(w, '.')); - for(int y_=0; y_ -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 vector & Expected, const vector & 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(_, CheckerExpansion().resultAfter(t, x0, y0, w, h));} -int main(){ - -CASE(0) - long long t = 1LL; - long long x0 = 0LL; - long long y0 = 0LL; - int w = 4; - int h = 4; - string __[] = {"....", "....", "....", "A..." }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(1) - long long t = 5LL; - long long x0 = 4LL; - long long y0 = 1LL; - int w = 3; - int h = 4; - string __[] = {"A..", "...", "B..", ".B." }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(2) - long long t = 1024LL; - long long x0 = 1525LL; - long long y0 = 512LL; - int w = 20; - int h = 2; - string __[] = {"B...B...B...........", ".B.A.B.A.B.........." }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(3) - long long t = 53LL; - long long x0 = 85LL; - long long y0 = 6LL; - int w = 5; - int h = 14; - string __[] = {".....", ".....", "B....", ".B.A.", ".....", ".....", ".....", ".....", ".....", ".....", "B....", ".B...", "..B..", ".A.B." }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(4) - long long t = 1000000000000LL; - long long x0 = 1000000000000LL; - long long y0 = 123456789123LL; - int w = 50; - int h = 50; - string __[] = {"?"}; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -/* -CASE(5) - long long t = LL; - long long x0 = LL; - long long y0 = LL; - int w = ; - int h = ; - string __[] = ; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -*/ -} -// END CUT HERE DELETED SRM/550-U/1C.cpp Index: SRM/550-U/1C.cpp ================================================================== --- SRM/550-U/1C.cpp +++ SRM/550-U/1C.cpp @@ -1,198 +0,0 @@ -#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 = 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 = LL(x.val)*y.val; } -mint operator+(mint x, mint y) { return x+=y; } -mint operator*(mint x, mint y) { return x*=y; } - -template -vector MATMUL(const vector< vector >& a, const vector& v) -{ - int N = a.size(); - vector u(N); - for(int i=0; i -vector< vector > MATMUL(const vector< vector >& a, const vector< vector >& b) -{ - int N = a.size(); - vector< vector > c(N, vector(N)); - for(int i=0; i -vector< vector > MATPOW(vector< vector > a, LL e) -{ - int N = a.size(); - vector< vector > c(N, vector(N)); - for(int i=0; i>=1) { - if(e&1) - c = MATMUL(c, a); - a = MATMUL(a, a); - } - return c; -} - -class ConversionMachine { public: - int countAll(string word1, string word2, vector costs, int maxCost) - { - int d[3] = {}; - LL minCost = 0; - for(int i=0; i maxCost ) - return 0; - LL c3 = accumulate(costs.begin(), costs.end(), 0LL); - return solve(d, (maxCost-minCost)/c3*3+d[1]*1+d[2]*2).val; - } - - mint solve(int d[3], int rem) - { - int n = d[0]+d[1]+d[2]; - - map, int> enc; - for(int b=0,id=0; b<=n; ++b) - for(int c=0; b+c<=n; ++c,++id) - enc[make_pair(b,c)] = id; - int D = enc.size(); - - vector< vector > M(D+1, vector(D+1)); - for(int b=0; b<=n; ++b) - for(int c=0; b+c<=n; ++c) - { - int a = n-b-c; - if(a) { - M[enc[make_pair(b,c+1)]][enc[make_pair(b,c)]] = a; - } - if(b) { - M[enc[make_pair(b-1,c)]][enc[make_pair(b,c)]] = b; - if(b-1==0 && c==0) - M.back()[enc[make_pair(b,c)]] = b; - } - if(c) { - M[enc[make_pair(b+1,c-1)]][enc[make_pair(b,c)]] = c; - } - } - M.back().back() = 1; - - vector V(D+1); - V[enc[make_pair(d[1],d[2])]] = 1; - V.back() = V[0]; - - return MATMUL(MATPOW(M, rem), V).back(); - } -}; - -// 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(_, ConversionMachine().countAll(word1, word2, costs, maxCost));} -int main(){ - -CASE(0) - string word1 = "a"; - string word2 = "b"; - int costs_[] = {100,2,3}; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = 205; - int _ = 2; -END -CASE(1) - string word1 = "abcba"; - string word2 = "abcba"; - int costs_[] = {67,23,43}; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = 0; - int _ = 1; -END -CASE(2) - string word1 = "cccccccc"; - string word2 = "aaaaaaaa"; - int costs_[] = {10000000,1,1}; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = 100; - int _ = 40320; -END -CASE(3) - string word1 = "aa"; - string word2 = "cc"; - int costs_[] = {1,10,100}; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = 1787; - int _ = 123611681; -END -/* -CASE(4) - string word1 = ; - string word2 = ; - int costs_[] = ; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = ; - int _ = ; -END -CASE(5) - string word1 = ; - string word2 = ; - int costs_[] = ; - vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); - int maxCost = ; - int _ = ; -END -*/ -} -// END CUT HERE ADDED SRM/550/1A.cpp Index: SRM/550/1A.cpp ================================================================== --- SRM/550/1A.cpp +++ SRM/550/1A.cpp @@ -0,0 +1,186 @@ +#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 RotatingBot { public: + int minArea(vector moves) + { + if( moves.size() == 1 ) { + int w = moves[0]+1; + return w; + } + if( moves.size() == 2 ) { + int w = moves[0]+1; + int h = moves[1]+1; + return w*h; + } + if( moves.size() == 3 ) { + int w1 = moves[0]+1; + int h = moves[1]+1; + int w2 = moves[2]+1; + return max(w1,w2)*h; + } + if( moves.size() == 4 ) { + int w1 = moves[0]+1; + int h1 = moves[1]+1; + int w2 = moves[2]+1; + int h2 = moves[3]+1; + if( w1 > w2 ) + return -1; + if( w1 == w2 ) { + if( h1 <= h2 ) + return -1; + else + return w1*h1; + } + return w2*max(h1,h2); + } + + int w1 = moves[0]+1; + int h1 = moves[1]+1; + int w2 = moves[2]+1; + int h2 = moves[3]+1; + if( w1 > w2 ) + return -1; + if( w1 == w2 ) { + vector rm = simulate(w1, h1, 0, 0); + if( match(rm, moves) ) + return w1*h1; + else + return -1; + } else { + if( h2

rm = simulate(w2, h2, w2-w1, h2-h1); + if( match(rm, moves) ) + return w2*h2; + else + return -1; + } + } + + vector simulate(int w, int h, int x, int y) + { + vector moves; + vector< vector > vis(h, vector(w)); + int dx[]={+1,0,-1,0}; + int dy[]={0,+1,0,-1}; + int d = 0; + vis[y][x] = true; + for(;;) { + int cnt = 0; + for(;;) { + int xx=x+dx[d]; + int yy=y+dy[d]; + if( yy<0||h<=yy||xx<0||w<=xx||vis[yy][xx] ) + break; + y=yy, x=xx; + vis[y][x] = true; + ++cnt; + } + d = (d+1)%4; + if( cnt == 0 ) + break; + moves.push_back(cnt); + } + return moves; + } + + bool match(const vector& rm, const vector& m) + { + if(rm.size() < m.size()) + return false; + for(int i=0; i+1= m[m.size()-1]; + } +}; + +// 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(_, RotatingBot().minArea(moves));} +int main(){ + +CASE(0) + int moves_[] = {15}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 16; +END +CASE(1) + int moves_[] = {3,10}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 44; +END +CASE(2) + int moves_[] = {1,1,1,1}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = -1; +END +CASE(3) + int moves_[] = {9,5,11,10,11,4,10}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 132; +END +CASE(4) + int moves_[] = {12,1,27,14,27,12,26,11,25,10,24,9,23,8,22,7,21,6,20,5,19,4,18,3,17,2,16,1,15}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 420; +END +CASE(5) + int moves_[] = {8,6,6,1}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = -1; +END +CASE(6) + int moves_[] = {8,6,6}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 63; +END +CASE(7) + int moves_[] = {5,4,5,3,3}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 30; +END +CASE(8) +int moves_[] = {2,2,2,1}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 9; +END +/* +CASE(9) + int moves_[] = ; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/550/1B.cpp Index: SRM/550/1B.cpp ================================================================== --- SRM/550/1B.cpp +++ SRM/550/1B.cpp @@ -0,0 +1,126 @@ +#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 CheckerExpansion { public: + vector resultAfter(long long t, long long x0, long long y0, int w, int h) + { + vector answer(h, string(w, '.')); + for(int y_=0; y_ +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 vector & Expected, const vector & 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(_, CheckerExpansion().resultAfter(t, x0, y0, w, h));} +int main(){ + +CASE(0) + long long t = 1LL; + long long x0 = 0LL; + long long y0 = 0LL; + int w = 4; + int h = 4; + string __[] = {"....", "....", "....", "A..." }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + long long t = 5LL; + long long x0 = 4LL; + long long y0 = 1LL; + int w = 3; + int h = 4; + string __[] = {"A..", "...", "B..", ".B." }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + long long t = 1024LL; + long long x0 = 1525LL; + long long y0 = 512LL; + int w = 20; + int h = 2; + string __[] = {"B...B...B...........", ".B.A.B.A.B.........." }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + long long t = 53LL; + long long x0 = 85LL; + long long y0 = 6LL; + int w = 5; + int h = 14; + string __[] = {".....", ".....", "B....", ".B.A.", ".....", ".....", ".....", ".....", ".....", ".....", "B....", ".B...", "..B..", ".A.B." }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + long long t = 1000000000000LL; + long long x0 = 1000000000000LL; + long long y0 = 123456789123LL; + int w = 50; + int h = 50; + string __[] = {"?"}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + long long t = LL; + long long x0 = LL; + long long y0 = LL; + int w = ; + int h = ; + string __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/550/1C.cpp Index: SRM/550/1C.cpp ================================================================== --- SRM/550/1C.cpp +++ SRM/550/1C.cpp @@ -0,0 +1,198 @@ +#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 = 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 = LL(x.val)*y.val; } +mint operator+(mint x, mint y) { return x+=y; } +mint operator*(mint x, mint y) { return x*=y; } + +template +vector MATMUL(const vector< vector >& a, const vector& v) +{ + int N = a.size(); + vector u(N); + for(int i=0; i +vector< vector > MATMUL(const vector< vector >& a, const vector< vector >& b) +{ + int N = a.size(); + vector< vector > c(N, vector(N)); + for(int i=0; i +vector< vector > MATPOW(vector< vector > a, LL e) +{ + int N = a.size(); + vector< vector > c(N, vector(N)); + for(int i=0; i>=1) { + if(e&1) + c = MATMUL(c, a); + a = MATMUL(a, a); + } + return c; +} + +class ConversionMachine { public: + int countAll(string word1, string word2, vector costs, int maxCost) + { + int d[3] = {}; + LL minCost = 0; + for(int i=0; i maxCost ) + return 0; + LL c3 = accumulate(costs.begin(), costs.end(), 0LL); + return solve(d, (maxCost-minCost)/c3*3+d[1]*1+d[2]*2).val; + } + + mint solve(int d[3], int rem) + { + int n = d[0]+d[1]+d[2]; + + map, int> enc; + for(int b=0,id=0; b<=n; ++b) + for(int c=0; b+c<=n; ++c,++id) + enc[make_pair(b,c)] = id; + int D = enc.size(); + + vector< vector > M(D+1, vector(D+1)); + for(int b=0; b<=n; ++b) + for(int c=0; b+c<=n; ++c) + { + int a = n-b-c; + if(a) { + M[enc[make_pair(b,c+1)]][enc[make_pair(b,c)]] = a; + } + if(b) { + M[enc[make_pair(b-1,c)]][enc[make_pair(b,c)]] = b; + if(b-1==0 && c==0) + M.back()[enc[make_pair(b,c)]] = b; + } + if(c) { + M[enc[make_pair(b+1,c-1)]][enc[make_pair(b,c)]] = c; + } + } + M.back().back() = 1; + + vector V(D+1); + V[enc[make_pair(d[1],d[2])]] = 1; + V.back() = V[0]; + + return MATMUL(MATPOW(M, rem), V).back(); + } +}; + +// 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(_, ConversionMachine().countAll(word1, word2, costs, maxCost));} +int main(){ + +CASE(0) + string word1 = "a"; + string word2 = "b"; + int costs_[] = {100,2,3}; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = 205; + int _ = 2; +END +CASE(1) + string word1 = "abcba"; + string word2 = "abcba"; + int costs_[] = {67,23,43}; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = 0; + int _ = 1; +END +CASE(2) + string word1 = "cccccccc"; + string word2 = "aaaaaaaa"; + int costs_[] = {10000000,1,1}; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = 100; + int _ = 40320; +END +CASE(3) + string word1 = "aa"; + string word2 = "cc"; + int costs_[] = {1,10,100}; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = 1787; + int _ = 123611681; +END +/* +CASE(4) + string word1 = ; + string word2 = ; + int costs_[] = ; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = ; + int _ = ; +END +CASE(5) + string word1 = ; + string word2 = ; + int costs_[] = ; + vector costs(costs_, costs_+sizeof(costs_)/sizeof(*costs_)); + int maxCost = ; + int _ = ; +END +*/ +} +// END CUT HERE