Artifact Content
Not logged in

Artifact 1b05819e2eb7f37b08554788f76ad719bb32c6b7


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

bool w[500][500];
bool v[500][500];

class DoNotTurn {
public:
	int minimumTurns(int N, int X0, int A, int B, int Y0, int C, int D, int P, int M) 
	{
		// input
		memset(w, 0, sizeof w);
		memset(v, 0, sizeof v);

		LL X=X0%P, Y=Y0%P;
		for(int i=1; i<=M; ++i)
		{
			if( !((X%N)==0 && (Y%N)==0 || (X%N)==N-1 && (Y%N)==N-1) )
				w[X%N][Y%N] = 1;
			X = (X*A+B) % P;
			Y = (Y*C+D) % P;
		}

		// solve
		vector< pair<int,int> > Q(1, make_pair(0,0)); v[0][0]=1;
		for(int step=-1; !Q.empty(); ++step)
		{
			vector< pair<int,int> > Q2;
			for(int i=0; i<Q.size(); ++i)
			{
				int x = Q[i].first;
				int y = Q[i].second;
				if( x==N-1 && y==N-1 )
					return step;

				int dx[] = {-1, +1, 0, 0};
				int dy[] = {0, 0, -1, +1};
				for(int d=0; d<4; ++d)
				{
					for(int k=1 ;; ++k) {
						int xx = x + dx[d]*k;
						int yy = y + dy[d]*k;
						if( xx<0 || N<=xx || yy<0 || N<=yy || w[xx][yy] )
							break;
						if( !v[xx][yy] ) {
							v[xx][yy] = true;
							Q2.push_back( make_pair(xx,yy) );
						}
					}
				}
			}
			Q.swap(Q2);
		}
		return -1;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int N = 2; 
	int X0 = 0; 
	int A = 0; 
	int B = 1; 
	int Y0 = 0; 
	int C = 0; 
	int D = 1; 
	int P = 10; 
	int M = 2; 
	int RetVal = 1; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<1>) {
	int N = 3; 
	int X0 = 0; 
	int A = 1; 
	int B = 1; 
	int Y0 = 1; 
	int C = 1; 
	int D = 0; 
	int P = 3; 
	int M = 3; 
	int RetVal = -1; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<2>) {
	int N = 3; 
	int X0 = 0; 
	int A = 1; 
	int B = 1; 
	int Y0 = 1; 
	int C = 1; 
	int D = 1; 
	int P = 3; 
	int M = 3; 
	int RetVal = 3; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<3>) {
	int N = 10; 
	int X0 = 911111; 
	int A = 845499; 
	int B = 866249; 
	int Y0 = 688029; 
	int C = 742197; 
	int D = 312197; 
	int P = 384409; 
	int M = 40; 
	int RetVal = 12; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<4>) {
	int N = 5; 
	int X0 = 23; 
	int A = 2; 
	int B = 3; 
	int Y0 = 35; 
	int C = 5; 
	int D = 7; 
	int P = 9; 
	int M = 3; 
	int RetVal = 2; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<5>) {
	int N = 2; 
	int X0 = 0; 
	int A = 0; 
	int B = 0; 
	int Y0 = 0; 
	int C = 0; 
	int D = 0; 
	int P = 1; 
	int M = 0; 
	int RetVal = 1; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }
int Test_(Case_<6>) {
	int N = 500; 
	int X0 = 0; 
	int A = 0; 
	int B = 0; 
	int Y0 = 0; 
	int C = 0; 
	int D = 0; 
	int P = 1; 
	int M = 0; 
	int RetVal = 1; 
	return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE