ADDED SRM/701-U/1A.cpp Index: SRM/701-U/1A.cpp ================================================================== --- SRM/701-U/1A.cpp +++ SRM/701-U/1A.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; + +class PartisanGame { public: + string getWinner(int n, vector a, vector b) + { + return firstPlayerWin(n, a, b) ? "Alice" : "Bob"; + } + + bool firstPlayerWin(int n, const vector& a, const vector& b) { + vector aw_bw; + aw_bw.push_back(0*2 + 0); + + for (int k = 1; k <= 2000; ++k) { + int aw = 0; + for (int ai : a) + if (k - ai >= 0 && (aw_bw[k - ai] & 1) == 0) + aw = 1; + int bw = 0; + for (int bi : b) + if (k - bi >= 0 && (aw_bw[k - bi] & 2) == 0) + bw = 1; + aw_bw.push_back(aw * 2 + bw); + } + if (n < aw_bw.size()) { + return (aw_bw[n] & 2) != 0; + } + + int z = int(aw_bw.size()) - 5; + for (int t = z - 1;; --t) { + if (equal(aw_bw.begin() + t, aw_bw.begin() + t + 5, aw_bw.begin() + z)) { + n = (n - t) % (z - t) + t; + return (aw_bw[n] & 2) != 0; + } + } + assert(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 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(_, PartisanGame().getWinner(n, a, b));} +int main(){ + +CASE(0) + int n = 7; + int a_[] = {3, 4}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {4}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Alice"; +END +CASE(1) + int n = 10; + int a_[] = {1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {4, 3, 2}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Bob"; +END +CASE(2) + int n = 104982; + int a_[] = {2, 3, 4, 5}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {2, 5}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Alice"; +END +CASE(3) + int n = 999999999; + int a_[] = {4}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {5}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Bob"; +END +CASE(4) + int n = 1000000000; + int a_[] = {1,2,3,4,5}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1,2,3,4,5}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Alice"; +END +CASE(5) + int n = 0; +int a_[] = { 1 }; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = { 1 }; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Bob"; +END +CASE(6) + int n = 1; +int a_[] = { 2,3,4 }; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = { 2 }; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Bob"; +END +} +// END CUT HERE ADDED SRM/701-U/1B.cpp Index: SRM/701-U/1B.cpp ================================================================== --- SRM/701-U/1B.cpp +++ SRM/701-U/1B.cpp @@ -0,0 +1,148 @@ +#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; + +string S; + +class KthStringAgain { +public: + struct Cand { + // represents ALL(S[perm_head..$]) S[tail_mask]^r + int perm_head; + string tail; + LL num; + Cand(int p, const string& t, LL n=1) : perm_head(p), tail(t), num(n) {} + LL count() const { return (1LL << (S.size() - perm_head))*num; } + + bool operator<(const Cand& rhs) const { + if (perm_head != rhs.perm_head) return perm_head < rhs.perm_head; + return tail < rhs.tail; + } + bool operator==(const Cand& rhs) const { + if (perm_head != rhs.perm_head) return false; + return tail == rhs.tail; + } + }; + + string getKth(string s, long long k) { + S = s; + vector cc(1, Cand(0, string())); + return findKthAmong(cc, k - 1); + } + + string findKthAmong(vector& c, LL k) { + sort(c.begin(), c.end()); + for(int i=c.size()-1; i>=1; --i) + if (c[i] == c[i - 1]) { + c[i - 1].num += c[i].num; + c[i].num = 0; + } + + map> next; + for (const Cand& cc : c) { + if (cc.num == 0) + continue; + + if (cc.perm_head == S.size()) { + if (cc.tail.empty()) + return ""; //? + + next[cc.tail[0]].emplace_back(cc.perm_head, cc.tail.substr(1), cc.num); + } + else { + for (int i = cc.perm_head; i < S.size(); ++i) { + string t2 = S.substr(cc.perm_head, i - cc.perm_head); + reverse(t2.begin(), t2.end()); + t2 += cc.tail; + next[S[i]].emplace_back(i + 1, t2, cc.num * (i+1==S.size() ? 2 : 1)); + } + } + } + + LL off = 0; + for (auto&& kv : next) { + LL num = 0; + for (auto&& vv : kv.second) + num += vv.count(); + if (off <= k && k < off + num) { + return kv.first + findKthAmong(kv.second, k - off); + } + off += num; + } + assert(false); + return "WRONG ANSWER"; + } +}; + +// 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(_, KthStringAgain().getKth(s, k));} +int main(){ + +CASE(0) + string s = "xyz"; + long long k = 5LL; + string _ = "yzx"; +END +CASE(1) + string s = "abc"; + long long k = 1LL; + string _ = "abc"; +END +CASE(2) + string s = "abc"; + long long k = 8LL; + string _ = "cba"; +END +CASE(3) + string s = "topcoder"; + long long k = 58LL; + string _ = "ooredcpt"; +END +CASE(4) + string s = "aaaabcdeeeghhhhhiijjjkllmmooooqqrrrrssttuuvvvvvwyy"; + long long k = 38517901796974LL; + string _ = "aaaacdeehhhijklmmqqrsttvvvvwyyvuusrrrooooljjihhgeb"; +END +CASE(5) + string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + long long k = 1234567890LL; + string _ = "???????"; +END +/* +CASE(6) + string s = ; + long long k = LL; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/701-U/1C-U.cpp Index: SRM/701-U/1C-U.cpp ================================================================== --- SRM/701-U/1C-U.cpp +++ SRM/701-U/1C-U.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 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 +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(_, FibonacciStringSum().get(n, a, b));} +int main(){ + +CASE(0) + int n = 3; + int a = 0; + int b = 0; + int _ = 5; +END +CASE(1) + int n = 3; + int a = 0; + int b = 1; + int _ = 5; +END +CASE(2) + int n = 10; + int a = 10; + int b = 10; + int _ = 518500021; +END +CASE(3) + int n = 5000; + int a = 20; + int b = 20; + int _ = 880245669; +END +/* +CASE(4) + int n = ; + int a = ; + int b = ; + int _ = ; +END +CASE(5) + int n = ; + int a = ; + int b = ; + int _ = ; +END +*/ +} +// END CUT HERE Index: TZTesterSp/template.cpp ================================================================== --- TZTesterSp/template.cpp +++ TZTesterSp/template.cpp @@ -12,17 +12,17 @@ #include #include #include #include #include +#include using namespace std; typedef long long LL; -typedef long double LD; -typedef complex CMP; +typedef complex CMP; class $CLASSNAME$ { public: $RC$ $METHODNAME$($METHODPARMS$) { } }; $TESTCODE$