9befbbd1cf 2016-09-06 kinaba: #include <iostream> 9befbbd1cf 2016-09-06 kinaba: #include <sstream> 9befbbd1cf 2016-09-06 kinaba: #include <iomanip> 9befbbd1cf 2016-09-06 kinaba: #include <vector> 9befbbd1cf 2016-09-06 kinaba: #include <string> 9befbbd1cf 2016-09-06 kinaba: #include <map> 9befbbd1cf 2016-09-06 kinaba: #include <set> 9befbbd1cf 2016-09-06 kinaba: #include <algorithm> 9befbbd1cf 2016-09-06 kinaba: #include <numeric> 9befbbd1cf 2016-09-06 kinaba: #include <iterator> 9befbbd1cf 2016-09-06 kinaba: #include <functional> 9befbbd1cf 2016-09-06 kinaba: #include <complex> 9befbbd1cf 2016-09-06 kinaba: #include <queue> 9befbbd1cf 2016-09-06 kinaba: #include <stack> 9befbbd1cf 2016-09-06 kinaba: #include <cmath> 9befbbd1cf 2016-09-06 kinaba: #include <cassert> 9befbbd1cf 2016-09-06 kinaba: #include <tuple> 9befbbd1cf 2016-09-06 kinaba: using namespace std; 9befbbd1cf 2016-09-06 kinaba: typedef long long LL; 9befbbd1cf 2016-09-06 kinaba: typedef complex<double> CMP; 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: static const unsigned MODVAL = 1000000007; 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: class XorOrderDiv1 { public: 9befbbd1cf 2016-09-06 kinaba: int get(int m, int n, int a0, int b) 9befbbd1cf 2016-09-06 kinaba: { 9befbbd1cf 2016-09-06 kinaba: vector<int> a; 9befbbd1cf 2016-09-06 kinaba: for(int i=0; i<n; ++i) 9befbbd1cf 2016-09-06 kinaba: a.push_back(int((a0 + LL(i)*b) % (1LL<<m))); 9befbbd1cf 2016-09-06 kinaba: sort(a.begin(), a.end()); 9befbbd1cf 2016-09-06 kinaba: return solve(a, m); 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: int solve(const vector<int>& A, int M) 9befbbd1cf 2016-09-06 kinaba: { 9befbbd1cf 2016-09-06 kinaba: vector<int> d; 9befbbd1cf 2016-09-06 kinaba: return solve_rec(A, 0, A.size(), M-1, d); 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: int solve_rec(const vector<int>& A, int s, int e, int bit, vector<int>& d) 9befbbd1cf 2016-09-06 kinaba: { 9befbbd1cf 2016-09-06 kinaba: if(s==e) 9befbbd1cf 2016-09-06 kinaba: return 0; 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: if(bit == -1) { 9befbbd1cf 2016-09-06 kinaba: const int M = d.size(); 9befbbd1cf 2016-09-06 kinaba: LL q = 0; 9befbbd1cf 2016-09-06 kinaba: LL sigma = 0; 9befbbd1cf 2016-09-06 kinaba: for(int m=M-1; m>=0; --m) 9befbbd1cf 2016-09-06 kinaba: { 9befbbd1cf 2016-09-06 kinaba: LL qprev = q; 9befbbd1cf 2016-09-06 kinaba: LL dd = d[m]; 9befbbd1cf 2016-09-06 kinaba: q = dd*dd%MODVAL*(1LL<<(M-m-1)) + sigma*dd%MODVAL*(1LL<<(M-m-1)) + 2*qprev; 9befbbd1cf 2016-09-06 kinaba: sigma += dd; 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: return int(q % MODVAL); 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: int m=s; 9befbbd1cf 2016-09-06 kinaba: for(; m<e; ++m) 9befbbd1cf 2016-09-06 kinaba: if(A[m]&(1<<bit)) 9befbbd1cf 2016-09-06 kinaba: break; 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: int ans = 0; 9befbbd1cf 2016-09-06 kinaba: d.push_back(e-m); 9befbbd1cf 2016-09-06 kinaba: ans ^= solve_rec(A, s, m, bit-1, d); 9befbbd1cf 2016-09-06 kinaba: d.pop_back(); 9befbbd1cf 2016-09-06 kinaba: d.push_back(m-s); 9befbbd1cf 2016-09-06 kinaba: ans ^= solve_rec(A, m, e, bit-1, d); 9befbbd1cf 2016-09-06 kinaba: d.pop_back(); 9befbbd1cf 2016-09-06 kinaba: return ans; 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: }; 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: // BEGIN CUT HERE 9befbbd1cf 2016-09-06 kinaba: #include <ctime> 9befbbd1cf 2016-09-06 kinaba: double start_time; string timer() 9befbbd1cf 2016-09-06 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9befbbd1cf 2016-09-06 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9befbbd1cf 2016-09-06 kinaba: { os << "{ "; 9befbbd1cf 2016-09-06 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9befbbd1cf 2016-09-06 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9befbbd1cf 2016-09-06 kinaba: void verify_case(const int& Expected, const int& Received) { 9befbbd1cf 2016-09-06 kinaba: bool ok = (Expected == Received); 9befbbd1cf 2016-09-06 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9befbbd1cf 2016-09-06 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9befbbd1cf 2016-09-06 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9befbbd1cf 2016-09-06 kinaba: #define END verify_case(_, XorOrderDiv1().get(m, n, a0, b));} 9befbbd1cf 2016-09-06 kinaba: int main(){ 9befbbd1cf 2016-09-06 kinaba: 9befbbd1cf 2016-09-06 kinaba: CASE(0) 9befbbd1cf 2016-09-06 kinaba: int m = 2; 9befbbd1cf 2016-09-06 kinaba: int n = 3; 9befbbd1cf 2016-09-06 kinaba: int a0 = 0; 9befbbd1cf 2016-09-06 kinaba: int b = 1; 9befbbd1cf 2016-09-06 kinaba: int _ = 8; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: CASE(1) 9befbbd1cf 2016-09-06 kinaba: int m = 3; 9befbbd1cf 2016-09-06 kinaba: int n = 5; 9befbbd1cf 2016-09-06 kinaba: int a0 = 1; 9befbbd1cf 2016-09-06 kinaba: int b = 3; 9befbbd1cf 2016-09-06 kinaba: int _ = 48; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: CASE(2) 9befbbd1cf 2016-09-06 kinaba: int m = 16; 9befbbd1cf 2016-09-06 kinaba: int n = 100; 9befbbd1cf 2016-09-06 kinaba: int a0 = 41; 9befbbd1cf 2016-09-06 kinaba: int b = 5; 9befbbd1cf 2016-09-06 kinaba: int _ = 523436032; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: CASE(3) 9befbbd1cf 2016-09-06 kinaba: int m = 30; 9befbbd1cf 2016-09-06 kinaba: int n = 200000; 9befbbd1cf 2016-09-06 kinaba: int a0 = 399; 9befbbd1cf 2016-09-06 kinaba: int b = 18082016; 9befbbd1cf 2016-09-06 kinaba: int _ = 408585698; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: /* 9befbbd1cf 2016-09-06 kinaba: CASE(4) 9befbbd1cf 2016-09-06 kinaba: int m = ; 9befbbd1cf 2016-09-06 kinaba: int n = ; 9befbbd1cf 2016-09-06 kinaba: int a0 = ; 9befbbd1cf 2016-09-06 kinaba: int b = ; 9befbbd1cf 2016-09-06 kinaba: int _ = ; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: CASE(5) 9befbbd1cf 2016-09-06 kinaba: int m = ; 9befbbd1cf 2016-09-06 kinaba: int n = ; 9befbbd1cf 2016-09-06 kinaba: int a0 = ; 9befbbd1cf 2016-09-06 kinaba: int b = ; 9befbbd1cf 2016-09-06 kinaba: int _ = ; 9befbbd1cf 2016-09-06 kinaba: END 9befbbd1cf 2016-09-06 kinaba: */ 9befbbd1cf 2016-09-06 kinaba: } 9befbbd1cf 2016-09-06 kinaba: // END CUT HERE