4a28d1c3b1 2015-02-18 kinaba: #include <iostream> 4a28d1c3b1 2015-02-18 kinaba: #include <sstream> 4a28d1c3b1 2015-02-18 kinaba: #include <iomanip> 4a28d1c3b1 2015-02-18 kinaba: #include <vector> 4a28d1c3b1 2015-02-18 kinaba: #include <string> 4a28d1c3b1 2015-02-18 kinaba: #include <map> 4a28d1c3b1 2015-02-18 kinaba: #include <set> 4a28d1c3b1 2015-02-18 kinaba: #include <algorithm> 4a28d1c3b1 2015-02-18 kinaba: #include <numeric> 4a28d1c3b1 2015-02-18 kinaba: #include <iterator> 4a28d1c3b1 2015-02-18 kinaba: #include <functional> 4a28d1c3b1 2015-02-18 kinaba: #include <complex> 4a28d1c3b1 2015-02-18 kinaba: #include <queue> 4a28d1c3b1 2015-02-18 kinaba: #include <stack> 4a28d1c3b1 2015-02-18 kinaba: #include <cmath> 4a28d1c3b1 2015-02-18 kinaba: #include <cassert> 4a28d1c3b1 2015-02-18 kinaba: #include <tuple> 4a28d1c3b1 2015-02-18 kinaba: using namespace std; 4a28d1c3b1 2015-02-18 kinaba: typedef long long LL; 4a28d1c3b1 2015-02-18 kinaba: typedef complex<double> CMP; 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: class XorSequence { public: 4a28d1c3b1 2015-02-18 kinaba: long long getmax(int N, int sz, int A0, int A1, int P, int Q, int R) 4a28d1c3b1 2015-02-18 kinaba: { 4a28d1c3b1 2015-02-18 kinaba: vector<int> A(sz); 4a28d1c3b1 2015-02-18 kinaba: A[0] = A0; 4a28d1c3b1 2015-02-18 kinaba: A[1] = A1; 4a28d1c3b1 2015-02-18 kinaba: for(int i=2; i<sz; i++) 4a28d1c3b1 2015-02-18 kinaba: A[i] = int((A[i-2]*LL(P) + A[i-1]*LL(Q) + R) % N); 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: int LN = 1; 4a28d1c3b1 2015-02-18 kinaba: while((1<<LN) != N) ++LN; 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: return solve(LN, A); 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: LL solve(int LN, const vector<int>& A) 4a28d1c3b1 2015-02-18 kinaba: { 4a28d1c3b1 2015-02-18 kinaba: LL total = 0; 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: vector<vector<int>> Sec(1, A); 4a28d1c3b1 2015-02-18 kinaba: for(int Bit=LN-1; Bit>=0; --Bit) { 4a28d1c3b1 2015-02-18 kinaba: vector<vector<int>> Next_Sec; 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: LL p01=0, p10=0; 4a28d1c3b1 2015-02-18 kinaba: for(vector<int>& A: Sec) { 4a28d1c3b1 2015-02-18 kinaba: vector<int> A0, A1; 4a28d1c3b1 2015-02-18 kinaba: LL n0=0, n1=0; 4a28d1c3b1 2015-02-18 kinaba: for(int i=0; i<A.size(); ++i) { 4a28d1c3b1 2015-02-18 kinaba: if((1<<Bit)&A[i]) { 4a28d1c3b1 2015-02-18 kinaba: p01 += n0; 4a28d1c3b1 2015-02-18 kinaba: ++n1; 4a28d1c3b1 2015-02-18 kinaba: A1.push_back(A[i]); 4a28d1c3b1 2015-02-18 kinaba: } else { 4a28d1c3b1 2015-02-18 kinaba: p10 += n1; 4a28d1c3b1 2015-02-18 kinaba: ++n0; 4a28d1c3b1 2015-02-18 kinaba: A0.push_back(A[i]); 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: if(Bit) { 4a28d1c3b1 2015-02-18 kinaba: if(!A0.empty())Next_Sec.emplace_back(std::move(A0)); 4a28d1c3b1 2015-02-18 kinaba: if(!A1.empty())Next_Sec.emplace_back(std::move(A1)); 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: Sec.swap(Next_Sec); 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: total += max(p01, p10); 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: return total; 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: }; 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: // BEGIN CUT HERE 4a28d1c3b1 2015-02-18 kinaba: #include <ctime> 4a28d1c3b1 2015-02-18 kinaba: double start_time; string timer() 4a28d1c3b1 2015-02-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4a28d1c3b1 2015-02-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4a28d1c3b1 2015-02-18 kinaba: { os << "{ "; 4a28d1c3b1 2015-02-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4a28d1c3b1 2015-02-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4a28d1c3b1 2015-02-18 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4a28d1c3b1 2015-02-18 kinaba: bool ok = (Expected == Received); 4a28d1c3b1 2015-02-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4a28d1c3b1 2015-02-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4a28d1c3b1 2015-02-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4a28d1c3b1 2015-02-18 kinaba: #define END verify_case(_, XorSequence().getmax(N, sz, A0, A1, P, Q, R));} 4a28d1c3b1 2015-02-18 kinaba: int main(){ 4a28d1c3b1 2015-02-18 kinaba: 4a28d1c3b1 2015-02-18 kinaba: CASE(0) 4a28d1c3b1 2015-02-18 kinaba: int N = 4; 4a28d1c3b1 2015-02-18 kinaba: int sz = 6; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 3; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 2; 4a28d1c3b1 2015-02-18 kinaba: int P = 0; 4a28d1c3b1 2015-02-18 kinaba: int Q = 1; 4a28d1c3b1 2015-02-18 kinaba: int R = 3; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 8LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(1) 4a28d1c3b1 2015-02-18 kinaba: int N = 8; 4a28d1c3b1 2015-02-18 kinaba: int sz = 8; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 2; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 5; 4a28d1c3b1 2015-02-18 kinaba: int P = 3; 4a28d1c3b1 2015-02-18 kinaba: int Q = 1; 4a28d1c3b1 2015-02-18 kinaba: int R = 4; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 13LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(2) 4a28d1c3b1 2015-02-18 kinaba: int N = 8; 4a28d1c3b1 2015-02-18 kinaba: int sz = 7; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 3; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 0; 4a28d1c3b1 2015-02-18 kinaba: int P = 1; 4a28d1c3b1 2015-02-18 kinaba: int Q = 2; 4a28d1c3b1 2015-02-18 kinaba: int R = 4; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 12LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(3) 4a28d1c3b1 2015-02-18 kinaba: int N = 32; 4a28d1c3b1 2015-02-18 kinaba: int sz = 15; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 7; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 9; 4a28d1c3b1 2015-02-18 kinaba: int P = 11; 4a28d1c3b1 2015-02-18 kinaba: int Q = 2; 4a28d1c3b1 2015-02-18 kinaba: int R = 1; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 60LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(4) 4a28d1c3b1 2015-02-18 kinaba: int N = 131072; 4a28d1c3b1 2015-02-18 kinaba: int sz = 131072; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 7; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 7; 4a28d1c3b1 2015-02-18 kinaba: int P = 1; 4a28d1c3b1 2015-02-18 kinaba: int Q = 0; 4a28d1c3b1 2015-02-18 kinaba: int R = 0; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 0LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(5) 4a28d1c3b1 2015-02-18 kinaba: int N = 131072; 4a28d1c3b1 2015-02-18 kinaba: int sz = 131070; 4a28d1c3b1 2015-02-18 kinaba: int A0 = 411; 4a28d1c3b1 2015-02-18 kinaba: int A1 = 415; 4a28d1c3b1 2015-02-18 kinaba: int P = 398; 4a28d1c3b1 2015-02-18 kinaba: int Q = 463; 4a28d1c3b1 2015-02-18 kinaba: int R = 9191; 4a28d1c3b1 2015-02-18 kinaba: long long _ = 4302679760LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: /* 4a28d1c3b1 2015-02-18 kinaba: CASE(6) 4a28d1c3b1 2015-02-18 kinaba: int N = ; 4a28d1c3b1 2015-02-18 kinaba: int sz = ; 4a28d1c3b1 2015-02-18 kinaba: int A0 = ; 4a28d1c3b1 2015-02-18 kinaba: int A1 = ; 4a28d1c3b1 2015-02-18 kinaba: int P = ; 4a28d1c3b1 2015-02-18 kinaba: int Q = ; 4a28d1c3b1 2015-02-18 kinaba: int R = ; 4a28d1c3b1 2015-02-18 kinaba: long long _ = LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: CASE(7) 4a28d1c3b1 2015-02-18 kinaba: int N = ; 4a28d1c3b1 2015-02-18 kinaba: int sz = ; 4a28d1c3b1 2015-02-18 kinaba: int A0 = ; 4a28d1c3b1 2015-02-18 kinaba: int A1 = ; 4a28d1c3b1 2015-02-18 kinaba: int P = ; 4a28d1c3b1 2015-02-18 kinaba: int Q = ; 4a28d1c3b1 2015-02-18 kinaba: int R = ; 4a28d1c3b1 2015-02-18 kinaba: long long _ = LL; 4a28d1c3b1 2015-02-18 kinaba: END 4a28d1c3b1 2015-02-18 kinaba: */ 4a28d1c3b1 2015-02-18 kinaba: } 4a28d1c3b1 2015-02-18 kinaba: // END CUT HERE