ADDED SRM/523/1A.cpp Index: SRM/523/1A.cpp ================================================================== --- SRM/523/1A.cpp +++ SRM/523/1A.cpp @@ -0,0 +1,114 @@ +#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 CountingSeries { public: + long long countThem(long long a, long long b, long long c, long long d, long long upperBound) + { + LL cnt = a<=upperBound ? (upperBound-a)/b+1 : 0; + for(; c<=upperBound; c*=d) { + if( !in_arith(a, b, c) ) + ++cnt; + if( d == 1 ) + break; + } + return cnt; + } + + bool in_arith(LL a, LL b, LL v) const + { + return v-a>=0 && (v-a)%b==0; + } +}; + +// 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(_, CountingSeries().countThem(a, b, c, d, upperBound));} +int main(){ + +CASE(0) + long long a = 1LL; + long long b = 1LL; + long long c = 1LL; + long long d = 2LL; + long long upperBound = 1000LL; + long long _ = 1000LL; +END +CASE(1) + long long a = 3LL; + long long b = 3LL; + long long c = 1LL; + long long d = 2LL; + long long upperBound = 1000LL; + long long _ = 343LL; +END +CASE(2) + long long a = 40LL; + long long b = 77LL; + long long c = 40LL; + long long d = 100000LL; + long long upperBound = 40LL; + long long _ = 1LL; +END +CASE(3) + long long a = 452LL; + long long b = 24LL; + long long c = 4LL; + long long d = 5LL; + long long upperBound = 600LL; + long long _ = 10LL; +END +CASE(4) + long long a = 234LL; + long long b = 24LL; + long long c = 377LL; + long long d = 1LL; + long long upperBound = 10000LL; + long long _ = 408LL; +END +CASE(5) + long long a = 8LL; + long long b = 8LL; + long long c = 2LL; + long long d = 2LL; + long long upperBound = 80LL; + long long _ = 12LL; +END +CASE(6) + long long a = 1LL; + long long b = 12345LL; + long long c = 1LL; + long long d = 100000LL; + long long upperBound = 1000000000000LL; + long long _ = -1LL; +END +} +// END CUT HERE ADDED SRM/523/1B.cpp Index: SRM/523/1B.cpp ================================================================== --- SRM/523/1B.cpp +++ SRM/523/1B.cpp @@ -0,0 +1,131 @@ +#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) {} // x>=0 + mint(size_t x):val(x%MODVAL) {} // x>=0 + mint(LL x):val(x%MODVAL) {} // x>=0 +}; +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; } + +class BricksN { public: + int countStructures(int w, int h, int k) + { + memo1.clear(); + memo2.clear(); + return rec(w, h, k).val; + } + + map memo1; + mint rec(int w, int h, int k) + { + if( h == 0 ) return 1; + if( w <= 0 ) return 1; + + const int key = w*51+h; + if( memo1.count(key) ) + return memo1[key]; + + mint sum = 1; + for(int s=0; s memo2; + mint full(int w, int k) + { + if( w <= 0 ) return 1; + + const int key = w; + if( memo2.count(key) ) + return memo2[key]; + + mint sum = 0; + for(int ww=1; ww<=min(w,k); ++ww) + sum += full(w-ww, k); + return memo2[key] = sum; + } +}; + +// 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(_, BricksN().countStructures(w, h, k));} +int main(){ + +CASE(0) + int w = 3; + int h = 1; + int k = 3; + int _ = 13; +END +CASE(1) + int w = 3; + int h = 2; + int k = 3; + int _ = 83; +END +CASE(2) + int w = 1; + int h = 5; + int k = 1; + int _ = 6; +END +CASE(3) + int w = 10; + int h = 10; + int k = 3; + int _ = 288535435; +END +CASE(4) + int w = 50; + int h = 50; + int k = 50; + int _ = -1; +END +CASE(5) + int w = 1; + int h = 1; + int k = 1; + int _ = 1; +END + +} +// END CUT HERE ADDED SRM/523/1C.cpp Index: SRM/523/1C.cpp ================================================================== --- SRM/523/1C.cpp +++ SRM/523/1C.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class AlphabetPaths { public: + int H, W; + long long count(vector maze) + { + H = maze.size(); + W = maze[0].size(); + for(int y=0; y reachable; + travel(1, 1<::iterator it=reachable.begin(); it!=reachable.end(); ++it) + { + unordered_map::iterator kt = reachable.find((((1<<21)-1)^it->first) | (1<second) * kt->second; + } + } + return total; + } + + void travel(int n, int mask, int y, int x, const vector& maze, + unordered_map& reachable) + { + if( n == 11 ) { + reachable[mask]++; + return; + } + + static const int dy[] = {-1,+1,0,0}; + static const int dx[] = {0,0,-1,+1}; + for(int i=0; i<4; ++i) { + int yy = y+dy[i]; + int xx = x+dx[i]; + if( 0<=yy && yy +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(_, AlphabetPaths().count(maze));} +int main(){ + +CASE(0) + string maze_[] = {"ABCDEFZHIXKLMNOPQRST", + "...................V"}; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 2LL; +END +CASE(1) + string maze_[] = {".................VT.", + "....................", + "ABCDEFZHIXKLMNOPQRS.", + "..................S.", + ".................VT."}; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 0LL; +END +CASE(2) + string maze_[] = {"TBCDE.PQRSA", + "FZHIXKLMNOV"}; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 50LL; +END +CASE(3) + string maze_[] = {"ABCDEF.", + "V....Z.", + "T....H.", + "S....I.", + "R....X.", + "KLMNOPQ"}; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 4LL; +END +CASE(4) + string maze_[] = { + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + "ABCDEFZHIXKLMNOPQRSTV", + "HIXKLMNOPQRSTVABCDEFZ", + "OPQRSTVABCDEFZHIXKLMN", + }; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 6119782LL; +END +CASE(5) + string maze_[] = {"ABCDEFZHIKLMNOPQRSTVX", "FZHIKLMNOPQRSTVXABCDE", "LMNOPQRSTVXABCDEFZHIK", "QRSTVXABCDEFZHIKLMNOP", "XABCDEFZHIKLMNOPQRSTV", "EFZHIKLMNOPQRSTVXABCD", "KLMNOPQRSTVXABCDEFZHI", "PQRSTVXABCDEFZHIKLMNO", "VXABCDEFZHIKLMNOPQRST", "DEFZHIKLMNOPQRSTVXABC", "IKLMNOPQRSTVXABCDEFZH", "OPQRSTVXABCDEFZHIKLMN", "TVXABCDEFZHIKLMNOPQRS", "CDEFZHIKLMNOPQRSTVXAB", "HIKLMNOPQRSTVXABCDEFZ", "NOPQRSTVXABCDEFZHIKLM", "STVXABCDEFZHIKLMNOPQR", "BCDEFZHIKLMNOPQRSTVXA", "ZHIKLMNOPQRSTVXABCDEF", "MNOPQRSTVXABCDEFZHIKL", "RSTVXABCDEFZHIKLMNOPQ"}; + vector maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); + long long _ = 10530238LL; +END +} +// END CUT HERE Index: TZTesterSp/template.cpp ================================================================== --- TZTesterSp/template.cpp +++ TZTesterSp/template.cpp @@ -13,10 +13,16 @@ #include #include #include #include #include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif using namespace std; typedef long long LL; typedef complex CMP; class $CLASSNAME$ { public: