ADDED SRM/TCO19-3B-U/1A.cpp Index: SRM/TCO19-3B-U/1A.cpp ================================================================== --- SRM/TCO19-3B-U/1A.cpp +++ SRM/TCO19-3B-U/1A.cpp @@ -0,0 +1,174 @@ +#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 TwoLadders { public: + long long minSteps(int sx, int lx1, int lx2, vector X, vector Y) + { + map> y2x; + for (int i = 0; i < X.size(); ++i) + y2x[Y[i]].push_back(X[i]); + + map> y2mM; + for (auto kv : y2x) + y2mM[kv.first] = make_pair( + *min_element(kv.second.begin(), kv.second.end()), + *max_element(kv.second.begin(), kv.second.end()) + ); + + auto cur = y2mM.begin(); + + // best 2 move candidates in the prvious level + int p1 = sx; + int p2 = sx; + LL b1 = 0; + LL b2 = 0; + int yy = 0; + + if(cur->first == 0) { + int m = cur->second.first; + int M = cur->second.second; + ++cur; + p1 = m; + b1 = abs(sx - M) + abs(M - m); + p2 = M; + b2 = abs(sx - m) + abs(M - m); + } + + for (; cur != y2mM.end(); ++cur) { + int ydiff = cur->first - yy; + yy = cur->first; + int m = cur->second.first; + int M = cur->second.second; + + int prev_p1 = p1; + int prev_p2 = p2; + LL prev_b1 = b1; + LL prev_b2 = b2; + + p1 = m; + b1 = min({ + prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - M) + abs(M - m), + prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - M) + abs(M - m), + prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - M) + abs(M - m), + prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - M) + abs(M - m), + }); + p2 = M; + b2 = min({ + prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - m) + abs(M - m), + prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - m) + abs(M - m), + prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - m) + abs(M - m), + prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - m) + abs(M - m), + }); + } + return min(b1, b2); + } +}; + +// 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(_, TwoLadders().minSteps(sx, lx1, lx2, X, Y));} +int main(){ + +CASE(0) + int sx = 10; + int lx1 = 0; + int lx2 = 100; + int X_[] = {47, 42}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {0, 0}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = 37LL; +END +CASE(1) + int sx = 10; + int lx1 = 8; + int lx2 = 11; + int X_[] = {10, 12}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {1, 1}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = 5LL; +END +CASE(2) + int sx = 10; + int lx1 = 8; + int lx2 = 42; + int X_[] = {10, 12}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {1, 1}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = 7LL; +END +CASE(3) + int sx = 10; + int lx1 = 8; + int lx2 = 42; + int X_[] = {10, 100, 12}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {1, 0, 1}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = 181LL; +END +CASE(4) + int sx = 500000000; + int lx1 = 1; + int lx2 = 999999999; + int X_[] = {0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {1, 1, 2, 2, 3, 3, 4, 4, 7, 7, 999999947, 999999947, 900000047, 900000047}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = 8499999959LL; +END +/* +CASE(5) + int sx = ; + int lx1 = ; + int lx2 = ; + int X_[] = ; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = ; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = LL; +END +CASE(6) + int sx = ; + int lx1 = ; + int lx2 = ; + int X_[] = ; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = ; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + long long _ = LL; +END +*/ +} +// END CUT HERE ADDED SRM/TCO19-3B-U/1B.cpp Index: SRM/TCO19-3B-U/1B.cpp ================================================================== --- SRM/TCO19-3B-U/1B.cpp +++ SRM/TCO19-3B-U/1B.cpp @@ -0,0 +1,171 @@ +#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 PrefixComposite { public: + vector minMax(long long A, long long B) + { + LL r = solve(B); + if (r < A) + return vector(); + + LL L = A - 1, R = r; + while(R-L>1) { + LL C = (L + R) / 2; + LL c = solve(C); + if (c < A) + L = C; + else + R = c; + } + return vector({ R, r }); + } + + // max such value <= B + LL solve(LL B) { + vector bb; + for(; B!=0; B/=10) + bb.push_back(B % 10); + reverse(bb.begin(), bb.end()); + + function rec = [&](LL pref, int i) { + if (i == bb.size()) + return pref; + + LL pp = pref * 10 + bb[i]; + if(is_composite(pp)) { + LL r = rec(pp, i + 1); + if (r) + return r; + } + + for (int k = bb[i] - 1; k >= 0; --k) { + LL pp = pref * 10 + k; + if (is_composite(pp)) { + int n = bb.size() - i - 1; + // get max from pp ++ [0-9]*n + for (int j = 0; j < n; ++j) + if (is_composite(pp*10+9)) + pp = pp*10+9; + else + pp = pp*10+8; + return pp; + } + } + return 0LL; + }; + return rec(0, 0); + } + + bool is_composite(LL v) + { + if (v == 0) + return true; + for (LL p = 2; p*p <= v; ++p) + if (v%p == 0) + return true; + return false; + } +}; + +// 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 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(_, PrefixComposite().minMax(A, B));} +int main(){ + +CASE(0) + long long A = 1LL; + long long B = 3LL; + vector _; +END +CASE(1) + long long A = 1LL; + long long B = 4LL; + long long __[] = {4, 4 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + long long A = 123LL; + long long B = 838LL; + long long __[] = {400, 828 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + long long A = 409LL; + long long B = 87343LL; + long long __[] = {420, 87343 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + long long A = 979797LL; + long long B = 979898LL; + vector _; +END +CASE(5) + long long A = 600LL; + long long B = 703LL; + long long __[] = {600, 699 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(6) + long long A = 1LL; + long long B = 100000000000LL; + long long __[] = {4, 99999999999 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(7) + long long A = 37337999LL; + long long B = 37337999LL; + vector _; +END +CASE(8) + long long A = 22LL; + long long B = 39LL; + vector _; +END +CASE(9) + long long A = 600LL; + long long B = 699LL; + long long __[] = { 600, 699 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(10) + long long A = 1LL; + long long B = 1LL; + vector _; +END +CASE(10) +long long A = 1LL; +long long B = 6LL; +vector _; +END +} +// END CUT HERE ADDED SRM/TCO19-3B-U/1C-U.cpp Index: SRM/TCO19-3B-U/1C-U.cpp ================================================================== --- SRM/TCO19-3B-U/1C-U.cpp +++ SRM/TCO19-3B-U/1C-U.cpp @@ -0,0 +1,144 @@ +#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 unsigned MODVAL = 1000000007; +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; } +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; } + +class DiagonalColumn { public: + int countGrids(int H, int W) + { + mint ans = 1; // all black + for(int l=0; l dp(H+1); + dp[0] = f; + for (int i = 0; i < H + W - 1; ++i) { + vector dp_prev = dp; + vector dp(H+1); + for (int r = 0; r <= H; ++r) { + if (r == 0) { + accumulate(dp_prev.begin(), dp_prev.end(), mint(0)); + } + else { + if(r == H) + dp[r] = (i == H - 1 || i == H + W - 2 ? mint(0) : (dp_prev[r - 1]+ dp_prev[r]) / 2); + else + dp[r] = dp_prev[r - 1]; + } + } + } + return accumulate(dp.begin(), dp.begin() + H, mint(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 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(_, DiagonalColumn().countGrids(H, W));} +int main(){ + +CASE(0) + int H = 2; + int W = 2; + int _ = 12; +END +CASE(1) + int H = 2; + int W = 3; + int _ = 37; +END +CASE(2) + int H = 3; + int W = 2; + int _ = 28; +END +CASE(3) + int H = 1; + int W = 5; + int _ = 32; +END +CASE(4) + int H = 6; + int W = 1; + int _ = 64; +END +CASE(5) + int H = 38; + int W = 38; + int _ = 173889321; +END +/* +CASE(6) + int H = ; + int W = ; + int _ = ; +END +CASE(7) + int H = ; + int W = ; + int _ = ; +END +*/ +} +// END CUT HERE