ed7454da2a 2016-10-26 kinaba: #include <iostream> ed7454da2a 2016-10-26 kinaba: #include <sstream> ed7454da2a 2016-10-26 kinaba: #include <iomanip> ed7454da2a 2016-10-26 kinaba: #include <vector> ed7454da2a 2016-10-26 kinaba: #include <string> ed7454da2a 2016-10-26 kinaba: #include <map> ed7454da2a 2016-10-26 kinaba: #include <set> ed7454da2a 2016-10-26 kinaba: #include <algorithm> ed7454da2a 2016-10-26 kinaba: #include <numeric> ed7454da2a 2016-10-26 kinaba: #include <iterator> ed7454da2a 2016-10-26 kinaba: #include <functional> ed7454da2a 2016-10-26 kinaba: #include <complex> ed7454da2a 2016-10-26 kinaba: #include <queue> ed7454da2a 2016-10-26 kinaba: #include <stack> ed7454da2a 2016-10-26 kinaba: #include <cmath> ed7454da2a 2016-10-26 kinaba: #include <cassert> ed7454da2a 2016-10-26 kinaba: #include <tuple> ed7454da2a 2016-10-26 kinaba: using namespace std; ed7454da2a 2016-10-26 kinaba: typedef long long LL; ed7454da2a 2016-10-26 kinaba: typedef complex<double> CMP; ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: class PartisanGame { public: ed7454da2a 2016-10-26 kinaba: string getWinner(int n, vector <int> a, vector <int> b) ed7454da2a 2016-10-26 kinaba: { ed7454da2a 2016-10-26 kinaba: return firstPlayerWin(n, a, b) ? "Alice" : "Bob"; ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: bool firstPlayerWin(int n, const vector<int>& a, const vector<int>& b) { ed7454da2a 2016-10-26 kinaba: vector<int> aw_bw; ed7454da2a 2016-10-26 kinaba: aw_bw.push_back(0*2 + 0); ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: for (int k = 1; k <= 2000; ++k) { ed7454da2a 2016-10-26 kinaba: int aw = 0; ed7454da2a 2016-10-26 kinaba: for (int ai : a) ed7454da2a 2016-10-26 kinaba: if (k - ai >= 0 && (aw_bw[k - ai] & 1) == 0) ed7454da2a 2016-10-26 kinaba: aw = 1; ed7454da2a 2016-10-26 kinaba: int bw = 0; ed7454da2a 2016-10-26 kinaba: for (int bi : b) ed7454da2a 2016-10-26 kinaba: if (k - bi >= 0 && (aw_bw[k - bi] & 2) == 0) ed7454da2a 2016-10-26 kinaba: bw = 1; ed7454da2a 2016-10-26 kinaba: aw_bw.push_back(aw * 2 + bw); ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: if (n < aw_bw.size()) { ed7454da2a 2016-10-26 kinaba: return (aw_bw[n] & 2) != 0; ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: int z = int(aw_bw.size()) - 5; ed7454da2a 2016-10-26 kinaba: for (int t = z - 1;; --t) { ed7454da2a 2016-10-26 kinaba: if (equal(aw_bw.begin() + t, aw_bw.begin() + t + 5, aw_bw.begin() + z)) { ed7454da2a 2016-10-26 kinaba: n = (n - t) % (z - t) + t; ed7454da2a 2016-10-26 kinaba: return (aw_bw[n] & 2) != 0; ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: assert(false); ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: }; ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: // BEGIN CUT HERE ed7454da2a 2016-10-26 kinaba: #include <ctime> ed7454da2a 2016-10-26 kinaba: double start_time; string timer() ed7454da2a 2016-10-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ed7454da2a 2016-10-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ed7454da2a 2016-10-26 kinaba: { os << "{ "; ed7454da2a 2016-10-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ed7454da2a 2016-10-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ed7454da2a 2016-10-26 kinaba: void verify_case(const string& Expected, const string& Received) { ed7454da2a 2016-10-26 kinaba: bool ok = (Expected == Received); ed7454da2a 2016-10-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ed7454da2a 2016-10-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ed7454da2a 2016-10-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ed7454da2a 2016-10-26 kinaba: #define END verify_case(_, PartisanGame().getWinner(n, a, b));} ed7454da2a 2016-10-26 kinaba: int main(){ ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: CASE(0) ed7454da2a 2016-10-26 kinaba: int n = 7; ed7454da2a 2016-10-26 kinaba: int a_[] = {3, 4}; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = {4}; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Alice"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(1) ed7454da2a 2016-10-26 kinaba: int n = 10; ed7454da2a 2016-10-26 kinaba: int a_[] = {1}; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = {4, 3, 2}; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Bob"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(2) ed7454da2a 2016-10-26 kinaba: int n = 104982; ed7454da2a 2016-10-26 kinaba: int a_[] = {2, 3, 4, 5}; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = {2, 5}; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Alice"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(3) ed7454da2a 2016-10-26 kinaba: int n = 999999999; ed7454da2a 2016-10-26 kinaba: int a_[] = {4}; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = {5}; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Bob"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(4) ed7454da2a 2016-10-26 kinaba: int n = 1000000000; ed7454da2a 2016-10-26 kinaba: int a_[] = {1,2,3,4,5}; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = {1,2,3,4,5}; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Alice"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(5) ed7454da2a 2016-10-26 kinaba: int n = 0; ed7454da2a 2016-10-26 kinaba: int a_[] = { 1 }; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = { 1 }; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Bob"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(6) ed7454da2a 2016-10-26 kinaba: int n = 1; ed7454da2a 2016-10-26 kinaba: int a_[] = { 2,3,4 }; ed7454da2a 2016-10-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); ed7454da2a 2016-10-26 kinaba: int b_[] = { 2 }; ed7454da2a 2016-10-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); ed7454da2a 2016-10-26 kinaba: string _ = "Bob"; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: // END CUT HERE