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: class CellScores { public: 4fd800b3a8 2011-02-23 kinaba: long long countScores(int N, int M, int K, int X0, int A, int B, int Y0, int C, int D) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> X(M+K,X0%N), Y(M+K,Y0%N); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<M+K; ++i) 4fd800b3a8 2011-02-23 kinaba: X[i] = (X[i-1]*A+B)%N, 4fd800b3a8 2011-02-23 kinaba: Y[i] = (Y[i-1]*C+D)%N; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<bool> > black(N, vector<bool>(N, false)); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<M; ++i) 4fd800b3a8 2011-02-23 kinaba: black[Y[i]][X[i]] = true; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // how to compute them???? 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > score_b(N, vector<int>(N, 0)); // # of gd rects around(Y,X) whose bottom line is y=Y+1 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > score_t(N, vector<int>(N, 0)); // top line is y=Y 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > score_l(N, vector<int>(N, 0)); // ... 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > score_r(N, vector<int>(N, 0)); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // O(1) score computation 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > score(N, vector<int>(N, 0)); 4fd800b3a8 2011-02-23 kinaba: score[0][0] = score_t[0][0]; 4fd800b3a8 2011-02-23 kinaba: for(int x=1; x<N; ++x) 4fd800b3a8 2011-02-23 kinaba: score[0][x] = score[0][x-1] - score_r[0][x-x] + score_l[0][x]; 4fd800b3a8 2011-02-23 kinaba: for(int y=1; y<N; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<N; ++x) 4fd800b3a8 2011-02-23 kinaba: score[y][x] = score[y-1][x] - score_b[y-1][x] + score_t[y][x]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ss = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=M; i<M+K; ++i) 4fd800b3a8 2011-02-23 kinaba: ss += score[Y[i]][X[i]]; 4fd800b3a8 2011-02-23 kinaba: return ss; 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, CellScores().countScores(N, M, K, X0, A, B, Y0, C, D));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int M = 0; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 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 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 100LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int N = 10; 4fd800b3a8 2011-02-23 kinaba: int M = 1; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int X0 = 5; 4fd800b3a8 2011-02-23 kinaba: int A = 1; 4fd800b3a8 2011-02-23 kinaba: int B = 5; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 5; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 5; 4fd800b3a8 2011-02-23 kinaba: long long _ = 75LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int N = 7; 4fd800b3a8 2011-02-23 kinaba: int M = 4; 4fd800b3a8 2011-02-23 kinaba: int K = 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 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int D = 1; 4fd800b3a8 2011-02-23 kinaba: long long _ = 194LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int N = 23; 4fd800b3a8 2011-02-23 kinaba: int M = 10; 4fd800b3a8 2011-02-23 kinaba: int K = 30; 4fd800b3a8 2011-02-23 kinaba: int X0 = 26; 4fd800b3a8 2011-02-23 kinaba: int A = 48; 4fd800b3a8 2011-02-23 kinaba: int B = 76; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 231; 4fd800b3a8 2011-02-23 kinaba: int C = 463; 4fd800b3a8 2011-02-23 kinaba: int D = 707; 4fd800b3a8 2011-02-23 kinaba: long long _ = 8088LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int N = 211; 4fd800b3a8 2011-02-23 kinaba: int M = 30; 4fd800b3a8 2011-02-23 kinaba: int K = 12; 4fd800b3a8 2011-02-23 kinaba: int X0 = 3; 4fd800b3a8 2011-02-23 kinaba: int A = 35; 4fd800b3a8 2011-02-23 kinaba: int B = 82; 4fd800b3a8 2011-02-23 kinaba: int Y0 = 0; 4fd800b3a8 2011-02-23 kinaba: int C = 43; 4fd800b3a8 2011-02-23 kinaba: int D = 15; 4fd800b3a8 2011-02-23 kinaba: long long _ = 18196443LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int M = 0; 4fd800b3a8 2011-02-23 kinaba: int K = 100; 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: long long _ = 900LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE