ADDED SRM/576-U/1A.cpp Index: SRM/576-U/1A.cpp ================================================================== --- SRM/576-U/1A.cpp +++ SRM/576-U/1A.cpp @@ -0,0 +1,158 @@ +#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 ArcadeManao { public: + int shortestLadder(vector level, int coinRow, int coinColumn) + { + for(int L=0; L<=level.size(); ++L) + if(can(level, coinRow-1, coinColumn-1, L)) + return L; + return level.size(); + } + + bool can(vector level, int Cy, int Cx, int L) + { + int H = level.size(); + int W = level[0].size(); + typedef pair point; + set V; + queue Q; + Q.push(point(H-1, 0)); + V.insert(Q.front()); + while(!Q.empty()) { + point p = Q.front(); + int y=p.first, x=p.second; + Q.pop(); + if(y==Cy && x==Cx) + return true; + vector cand; + cand.push_back(point(y,x-1)); + cand.push_back(point(y,x+1)); + for(int dy=-L; dy<=+L; ++dy) + cand.push_back(point(y+dy, x)); + + 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 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(_, ArcadeManao().shortestLadder(level, coinRow, coinColumn));} +int main(){ + +CASE(0) + string level_[] = {"XXXX....", + "...X.XXX", + "XXX..X..", + "......X.", + "XXXXXXXX"}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 2; + int coinColumn = 4; + int _ = 2; +END +CASE(1) + string level_[] = {"XXXX", + "...X", + "XXXX"}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 1; + int coinColumn = 1; + int _ = 1; +END +CASE(2) + string level_[] = {"..X..", + ".X.X.", + "X...X", + ".X.X.", + "..X..", + "XXXXX"}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 1; + int coinColumn = 3; + int _ = 4; +END +CASE(3) + string level_[] = {"X"}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 1; + int coinColumn = 1; + int _ = 0; +END +CASE(4) + string level_[] = {"XXXXXXXXXX", + "...X......", + "XXX.......", + "X.....XXXX", + "..XXXXX..X", + ".........X", + ".........X", + "XXXXXXXXXX"}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 1; + int coinColumn = 1; + int _ = 2; +END +CASE(5) + string level_[] = { + "XXXXXXXXXXXXXXX", + "..............X", + "..............X", + "..............X", + "XXXXXXXXXXXXXXX", +}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = 1; + int coinColumn = 1; + int _ = 1; +END +/* +CASE(6) + string level_[] = ; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int coinRow = ; + int coinColumn = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/576-U/1B-U.cpp Index: SRM/576-U/1B-U.cpp ================================================================== --- SRM/576-U/1B-U.cpp +++ SRM/576-U/1B-U.cpp @@ -0,0 +1,200 @@ +#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 unsigned MODVAL = 1000000009; +struct mint +{ + unsigned val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} + mint(unsigned 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 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; } + +template +struct DP4x +{ + int N1, N2, N3, N4; + vector data; + DP4x(int, int N2, int N3, int N4, const T& t = T()) + : N1(2), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert(data.size()*sizeof(T)<(1<<26)); } + T& operator()(int i1, int i2, int i3, int i4) + { i1&=1; return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } + void swap(DP4x& rhs) + { data.swap(rhs.data); } +}; + +class TheExperiment { public: + int countPlacements(vector intensity, int M, int L, int A, int B) + { + string S = accumulate(intensity.begin(), intensity.end(), string()); + int N = S.size(); + + vector range_L, range_R; // [L,R) + { + vector PS(N+1, 0); + for(int i=0; i dp(M+1, N+1, M, 2); + dp(0, 0, 0, 0) = 1; + 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 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(_, TheExperiment().countPlacements(intensity, M, L, A, B));} +int main(){ + +CASE(0) + string intensity_[] = {"341156"}; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = 3; + int L = 3; + int A = 6; + int B = 10; + int _ = 2; +END +CASE(1) + string intensity_[] = {"999112999"}; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = 2; + int L = 4; + int A = 21; + int B = 30; + int _ = 2; +END +CASE(2) + string intensity_[] = {"111"}; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = 2; + int L = 2; + int A = 2; + int B = 3; + int _ = 0; +END +CASE(3) + string intensity_[] = {"59059", "110", "1132230231"}; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = 1; + int L = 5; + int A = 10; + int B = 20; + int _ = 6; +END +CASE(4) + string intensity_[] = {"111111111111111111111111", "111111111111111111111111111", "11111111111111111111111"}; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = 12; + int L = 8; + int A = 4; + int B = 2700; + int _ = 418629948; +END +/* +CASE(5) + string intensity_[] = ; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = ; + int L = ; + int A = ; + int B = ; + int _ = ; +END +CASE(6) + string intensity_[] = ; + vector intensity(intensity_, intensity_+sizeof(intensity_)/sizeof(*intensity_)); + int M = ; + int L = ; + int A = ; + int B = ; + int _ = ; +END +*/ +} +// END CUT HERE