4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool w[500][500]; 4fd800b3a8 2011-02-23 kinaba: bool v[500][500]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class DoNotTurn { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int minimumTurns(int N, int X0, int A, int B, int Y0, int C, int D, int P, int M) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // input 4fd800b3a8 2011-02-23 kinaba: memset(w, 0, sizeof w); 4fd800b3a8 2011-02-23 kinaba: memset(v, 0, sizeof v); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL X=X0%P, Y=Y0%P; 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<=M; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( !((X%N)==0 && (Y%N)==0 || (X%N)==N-1 && (Y%N)==N-1) ) 4fd800b3a8 2011-02-23 kinaba: w[X%N][Y%N] = 1; 4fd800b3a8 2011-02-23 kinaba: X = (X*A+B) % P; 4fd800b3a8 2011-02-23 kinaba: Y = (Y*C+D) % P; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // solve 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > Q(1, make_pair(0,0)); v[0][0]=1; 4fd800b3a8 2011-02-23 kinaba: for(int step=-1; !Q.empty(); ++step) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > Q2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<Q.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int x = Q[i].first; 4fd800b3a8 2011-02-23 kinaba: int y = Q[i].second; 4fd800b3a8 2011-02-23 kinaba: if( x==N-1 && y==N-1 ) 4fd800b3a8 2011-02-23 kinaba: return step; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int dx[] = {-1, +1, 0, 0}; 4fd800b3a8 2011-02-23 kinaba: int dy[] = {0, 0, -1, +1}; 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<4; ++d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int k=1 ;; ++k) { 4fd800b3a8 2011-02-23 kinaba: int xx = x + dx[d]*k; 4fd800b3a8 2011-02-23 kinaba: int yy = y + dy[d]*k; 4fd800b3a8 2011-02-23 kinaba: if( xx<0 || N<=xx || yy<0 || N<=yy || w[xx][yy] ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: if( !v[xx][yy] ) { 4fd800b3a8 2011-02-23 kinaba: v[xx][yy] = true; 4fd800b3a8 2011-02-23 kinaba: Q2.push_back( make_pair(xx,yy) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: Q.swap(Q2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int X0 = 0; 4fd800b3a8 2011-02-23 kinaba: int A = 0; 4fd800b3a8 2011-02-23 kinaba: int B = 1; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 0; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: int P = 10; 4fd800b3a8 2011-02-23 kinaba: int M = 2; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int X0 = 0; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 1; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 1; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 0; 4fd800b3a8 2011-02-23 kinaba: int P = 3; 4fd800b3a8 2011-02-23 kinaba: int M = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = -1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int X0 = 0; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 1; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 1; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: int P = 3; 4fd800b3a8 2011-02-23 kinaba: int M = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 3; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int X0 = 911111; 4fd800b3a8 2011-02-23 kinaba: int A = 845499; 4fd800b3a8 2011-02-23 kinaba: int B = 866249; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 688029; 4fd800b3a8 2011-02-23 kinaba: int C = 742197; 4fd800b3a8 2011-02-23 kinaba: int D = 312197; 4fd800b3a8 2011-02-23 kinaba: int P = 384409; 4fd800b3a8 2011-02-23 kinaba: int M = 40; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 12; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int X0 = 23; 4fd800b3a8 2011-02-23 kinaba: int A = 2; 4fd800b3a8 2011-02-23 kinaba: int B = 3; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 35; 4fd800b3a8 2011-02-23 kinaba: int C = 5; 4fd800b3a8 2011-02-23 kinaba: int D = 7; 4fd800b3a8 2011-02-23 kinaba: int P = 9; 4fd800b3a8 2011-02-23 kinaba: int M = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int N = 2; 4fd800b3a8 2011-02-23 kinaba: int X0 = 0; 4fd800b3a8 2011-02-23 kinaba: int A = 0; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 0; 4fd800b3a8 2011-02-23 kinaba: int D = 0; 4fd800b3a8 2011-02-23 kinaba: int P = 1; 4fd800b3a8 2011-02-23 kinaba: int M = 0; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: int N = 500; 4fd800b3a8 2011-02-23 kinaba: int X0 = 0; 4fd800b3a8 2011-02-23 kinaba: int A = 0; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 0; 4fd800b3a8 2011-02-23 kinaba: int D = 0; 4fd800b3a8 2011-02-23 kinaba: int P = 1; 4fd800b3a8 2011-02-23 kinaba: int M = 0; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, D, P, M)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: