Artifact Content
Not logged in

Artifact 6420102b826df671b84826ae5d107117220ef3b6


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
static const int INF = 0x3fffffff;

class PrimeCompositeGame { public:
	int theOutcome(int N, int K)
	{
		enum {P, C};

		// sieve
		vector< vector<bool> > is(2, vector<bool>(N+1, true));
		is[P][0] = is[C][0] = is[P][1] = is[C][1] = false;
		for(int p=2; p<=N; ++p) {
			if( is[P][p] )
				for(int q=p+p; q<=N; q+=p)
					is[P][q] = false;
			is[C][p] = !is[P][p];
		}

		vector< vector<int> > score(2, vector<int>(N+1));
		multiset<int> lastK[2];
		for(int t=0; t<=N; ++t) {
			for(int me=0; me<2; ++me)
				if( lastK[1^me].empty() ) // no valid move. immediately lose.
					score[me][t] = 0;
				else if( *lastK[1^me].begin() > 0 ) // all move make opponent win. take the longest.
					score[me][t] = - (1 + *lastK[1^me].rbegin());
				else // I can win. take the shortest.
					score[me][t] = (1 + abs(*--lastK[1^me].lower_bound(1)));
			// maintain last K scores.
			for(int me=0; me<2; ++me) {
				if( is[1^me][t] )
					lastK[me].insert( score[me][t] );
				if( t-K>=0 && is[1^me][t-K] )
					lastK[me].erase(lastK[me].find(score[me][t-K]));
			}
		}
		return score[P][N];
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::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(_, PrimeCompositeGame().theOutcome(N, K));}
int main(){

CASE(0)
	int N = 3; 
	int K = 2; 
	int _ = 1; 
END
CASE(1)
	int N = 58; 
	int K = 1; 
	int _ = 0; 
END
CASE(2)
	int N = 30; 
	int K = 3; 
	int _ = -2; 
END
CASE(3)
	int N = 6; 
	int K = 3; 
	int _ = 1; 
END
CASE(4)
	int N = 526; 
	int K = 58; 
	int _ = 19; 
END
CASE(5)
	int N = 474747; 
	int K = 474747; 
	int _ = 1; 
END
/*
CASE(6)
	int N = ; 
	int K = ; 
	int _ = ; 
END
*/
}
// END CUT HERE