ADDED SRM/TCO21-3A-U/1A.cpp Index: SRM/TCO21-3A-U/1A.cpp ================================================================== --- SRM/TCO21-3A-U/1A.cpp +++ SRM/TCO21-3A-U/1A.cpp @@ -0,0 +1,132 @@ +#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; } + +vector FAC_(1, 1); +mint FAC(LL n) { while (FAC_.size() <= n) FAC_.push_back(FAC_.back()*LL(FAC_.size())); return FAC_[n]; } + +// nCk :: O(log MODVAL) time, O(n) space. +mint C(LL n, LL k) { return k<0 || n key(p, U, L, D); + if (memo.count(key)) + return memo[key]; + if (U == 0 && L == 0 && D == 0) + return 1; + mint ans = 0; + if (p != 0 && U != 0) + ans += solve(0, U - 1, L, D); + if (p != 1 && L != 0) + ans += solve(1, U, L - 1, D); + if (p != 2 && D != 0) + ans += solve(2, U, L, D - 1); + return memo[key] = ans.val; + } + + map, int> memo; +}; + +// 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(_, AnnoyingPasswords().count(U, L, D));} +int main(){ + +CASE(0) + int U = 4; + int L = 1; + int D = 1; + int _ = 0; +END +CASE(1) + int U = 5; + int L = 0; + int D = 4; + int _ = 783743727; +END +CASE(2) + int U = 1; + int L = 1; + int D = 1; + int _ = 40560; +END +CASE(3) + int U = 2; + int L = 2; + int D = 3; + int _ = 559599923; +END +CASE(4) + int U = 0; + int L = 0; + int D = 0; + int _ = 1; +END +CASE(5) + int U = 26; + int L = 26; + int D = 10; + int _ = 0; +END +CASE(6) + int U = 0; + int L = 0; + int D = 0; + int _ = 1; +END +} +// END CUT HERE ADDED SRM/TCO21-3A-U/1B-U.cpp Index: SRM/TCO21-3A-U/1B-U.cpp ================================================================== --- SRM/TCO21-3A-U/1B-U.cpp +++ SRM/TCO21-3A-U/1B-U.cpp @@ -0,0 +1,130 @@ +#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 TellBagsApart { public: + string whichBagIsSmaller(vector records) + { + srand(time(0)); + string ans; + for (int i = 0; i < records.size(); i += 8) + ans += char('0' + solve(vector(records.begin() + i, records.begin() + i + 8))); + return ans; + } + + int solve(vector r) { + // same = 3/7, diff = 4/7 + // same = 19/39, diff = 20/39 + int a1 = r[0] + r[1] + r[2] + r[3]; + int s1 = r[0] + r[3]; + int d1 = a1 - s1; + int a2 = r[4] + r[5] + r[6] + r[7]; + int s2 = r[4] + r[7]; + int d2 = a2 - s2; + int lhs, rhs; + if (a1 == 0) { + // s2/a2 closer to 19/39 ? 1 : 2 + // 125/273 < s2/a2 + lhs = 125 * a2; + rhs = s2 * 273; + } + else if (a2 == 0) { + // s1/a1 closer to 3/7 ? 1 : 2 + // s1/a1 < 125/273 + lhs = s1 * 273; + rhs = 125 * a1; + } + else { + // s1/a1 < s2/a2 ? 1 : 2 + lhs = s1 * a2; + rhs = s2 * a1; + } + + if (lhs == rhs) { + return (rand() >> 8) & 1 ? 1 : 2; + } + else { + return lhs < rhs ? 1 : 2; + } + } +}; + +// 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 string& Expected, const string& 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(_, TellBagsApart().whichBagIsSmaller(records));} +int main(){ + +CASE(0) + int records_[] = {262, 371, 340, 277, 303, 304, 333, 310, + 296, 326, 370, 275, 312, 329, 284, 308, + 265, 402, 372, 279, 279, 317, 307, 279, + 112, 160, 121, 102, 497, 497, 505, 506}; + vector records(records_, records_+sizeof(records_)/sizeof(*records_)); + string _ = "1111"; +END +CASE(1) + int records_[] = {401, 405, 345, 358, 203, 295, 284, 209, + 348, 380, 396, 361, 221, 274, 307, 213, + 361, 347, 410, 347, 246, 287, 298, 204, + 301, 389, 412, 304, 253, 289, 280, 272, + 303, 450, 388, 290, 270, 286, 246, 267, + 328, 354, 326, 362, 254, 305, 309, 262, + 290, 362, 391, 296, 285, 282, 313, 281, + 338, 335, 345, 335, 220, 338, 335, 254, + 309, 356, 348, 323, 239, 344, 343, 238, + 264, 368, 365, 258, 301, 312, 328, 304, + 256, 368, 343, 295, 296, 323, 319, 300, + 275, 318, 383, 258, 320, 340, 306, 300, + 275, 301, 323, 309, 273, 372, 366, 281, + 263, 331, 290, 309, 277, 358, 395, 277, + 261, 310, 291, 259, 301, 407, 379, 292, + 256, 318, 297, 257, 325, 358, 366, 323, + 284, 287, 274, 286, 294, 406, 358, 311, + 266, 271, 282, 256, 282, 395, 429, 319, + 270, 274, 278, 268, 308, 396, 404, 302, + 203, 283, 299, 229, 368, 401, 377, 340}; + vector records(records_, records_+sizeof(records_)/sizeof(*records_)); + string _ = "22211212211122212221"; +END +/* +CASE(2) + int records_[] = ; + vector records(records_, records_+sizeof(records_)/sizeof(*records_)); + string _ = ; +END +CASE(3) + int records_[] = ; + vector records(records_, records_+sizeof(records_)/sizeof(*records_)); + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/TCO21-3A-U/1C-U.cpp Index: SRM/TCO21-3A-U/1C-U.cpp ================================================================== --- SRM/TCO21-3A-U/1C-U.cpp +++ SRM/TCO21-3A-U/1C-U.cpp @@ -0,0 +1,132 @@ +#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 TowerPlacement { public: + int solve(int R, int C, int seed, int X) + { + vector qr(50000), qc(50000); + LL state = seed; + for (int i = 0; i < 50000; ++i) { + state = (state * 1103515245 + 12345) & 0x7fffffffLL; + qr[i] = (state / 1000) % R; + state = (state * 1103515245 + 12345) & 0x7fffffffLL; + qc[i] = (state / 1000) % C; + } + + vector rej(50000); + for (int r = 0; r < X; ++r) { + int L = 0, R = 50001; + while (R - L > 1) { + int M = (L + R) / 2; + (feasible(R,C,qr,qc,rej,M) ? L : R) = M; + } + if (L == 50000) + return -1; + if (r == X - 1) + return L; + rej[L] = true; + } + } + + // Is placing [0, M) feasible? + bool feasible(int R, int C, const vector& qr, const vector& qc, const vector& rej, int M) { + #define ENC(r,c) ((r)*C+(c)) + map t; + for (int i = 0; i < M; ++i) { + if (rej[i]) + continue; + int key = ENC(qr[i], qc[i]); + if (t.count(key)) + continue; + + t[key] = i; + + // conflict generation + qr[i]+1, qc[i] + } + // solve + return false; + #undef ENC + } +}; + +// 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(_, TowerPlacement().solve(R, C, seed, X));} +int main(){ + +CASE(0) + int R = 1; + int C = 1; + int seed = 47; + int X = 5; + int _ = 4; +END +CASE(1) + int R = 3; + int C = 3; + int seed = 47; + int X = 4; + int _ = 6; +END +CASE(2) + int R = 10000; + int C = 14700; + int seed = 12345; + int X = 1; + int _ = -1; +END +CASE(3) + int R = 10; + int C = 12; + int seed = 123456789; + int X = 3; + int _ = 19; +END +/* +CASE(4) + int R = ; + int C = ; + int seed = ; + int X = ; + int _ = ; +END +CASE(5) + int R = ; + int C = ; + int seed = ; + int X = ; + int _ = ; +END +*/ +} +// END CUT HERE