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 <functional> 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: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CMP tmp[65536*4]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void fft_impl( CMP a[], int stride, int n, double Wt ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( n > 1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: CMP *ev=a, *od=a+stride; 4fd800b3a8 2011-02-23 kinaba: int s2=stride*2, n2=n/2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: fft_impl( ev, s2, n2, Wt*2 ); 4fd800b3a8 2011-02-23 kinaba: fft_impl( od, s2, n2, Wt*2 ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) tmp[i] = ev[s2*(i%n2)] + od[s2*(i%n2)]*polar(1.0,i*Wt); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) a[stride*i] = tmp[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void fft( vector<CMP>& a ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: fft_impl(&a[0], 1, a.size(), +2*M_PI/a.size()); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void ifft( vector<CMP>& a ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: fft_impl(&a[0], 1, a.size(), -2*M_PI/a.size()); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<a.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: a[i] /= a.size(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class CircularShifts { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int maxScore(int N, int Z0, int A, int B, int M) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // input 4fd800b3a8 2011-02-23 kinaba: LL X[60000], Y[60000], Z=Z0%M; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N+N; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: (i<N ? X[i] : Y[i-N]) = Z % 100; 4fd800b3a8 2011-02-23 kinaba: Z = (Z*A+B) % M; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // solve 4fd800b3a8 2011-02-23 kinaba: int NN = 1; 4fd800b3a8 2011-02-23 kinaba: while( NN < N*2 ) NN*=2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<CMP> CX(NN), CY(NN), CZ(NN); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: CX[i]=CX[i+N]=X[N-i-1], CY[i]=Y[i]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: fft(CX); 4fd800b3a8 2011-02-23 kinaba: fft(CY); 4fd800b3a8 2011-02-23 kinaba: transform( CX.begin(), CX.end(), CY.begin(), CZ.begin(), multiplies<CMP>() ); 4fd800b3a8 2011-02-23 kinaba: ifft(CZ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double m = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<NN; ++i) 4fd800b3a8 2011-02-23 kinaba: m = max(m, CZ[i].real()); 4fd800b3a8 2011-02-23 kinaba: return int(m+0.5); 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 = 5; 4fd800b3a8 2011-02-23 kinaba: int Z0 = 1; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int M = 13; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int N = 4; 4fd800b3a8 2011-02-23 kinaba: int Z0 = 1; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 1; 4fd800b3a8 2011-02-23 kinaba: int M = 20; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 70; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int Z0 = 23; 4fd800b3a8 2011-02-23 kinaba: int A = 11; 4fd800b3a8 2011-02-23 kinaba: int B = 51; 4fd800b3a8 2011-02-23 kinaba: int M = 4322; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 28886; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int N = 1000; 4fd800b3a8 2011-02-23 kinaba: int Z0 = 3252; 4fd800b3a8 2011-02-23 kinaba: int A = 3458736; 4fd800b3a8 2011-02-23 kinaba: int B = 233421; 4fd800b3a8 2011-02-23 kinaba: int M = 111111111; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2585408; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int N = 141; 4fd800b3a8 2011-02-23 kinaba: int Z0 = 96478; 4fd800b3a8 2011-02-23 kinaba: int A = 24834; 4fd800b3a8 2011-02-23 kinaba: int B = 74860; 4fd800b3a8 2011-02-23 kinaba: int M = 92112; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 419992; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, 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: