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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename T = LL> 4fd800b3a8 2011-02-23 kinaba: struct FenwickTree 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<T> x; 4fd800b3a8 2011-02-23 kinaba: FenwickTree(size_t n, const T& v = T()) : x(n, v) {} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void incr( int k, const T& a ) { // z[k] += a; 4fd800b3a8 2011-02-23 kinaba: for(; k < x.size(); k|=k+1) 4fd800b3a8 2011-02-23 kinaba: x[k] += a; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: T sum(int i, int j) { // z[i]+...+z[j] : inclusive 4fd800b3a8 2011-02-23 kinaba: if(i) 4fd800b3a8 2011-02-23 kinaba: return sum(0, j) - sum(0, i-1); 4fd800b3a8 2011-02-23 kinaba: else { 4fd800b3a8 2011-02-23 kinaba: T v = T(); 4fd800b3a8 2011-02-23 kinaba: for(; j>=0; j=(j&(j+1))-1) 4fd800b3a8 2011-02-23 kinaba: v += x[j]; 4fd800b3a8 2011-02-23 kinaba: return v; 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: static const int MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class ProductOfPrices 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int product(int N, int L, int X0, int A, int B) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL result = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: FenwickTree<> fw(L), nm(L); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL X = X0 % L; 4fd800b3a8 2011-02-23 kinaba: fw.incr(X, X); 4fd800b3a8 2011-02-23 kinaba: nm.incr(X, 1); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: X = (X*A+B) % L; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL sumL = fw.sum(0, X-1); 4fd800b3a8 2011-02-23 kinaba: LL sumR = fw.sum(X, L-1); 4fd800b3a8 2011-02-23 kinaba: LL numL = nm.sum(0, X-1); 4fd800b3a8 2011-02-23 kinaba: LL numR = nm.sum(X, L-1); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL price = ((X*numL - sumL) + (sumR - X*numR)) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: result = (result * price) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: fw.incr(X, X); 4fd800b3a8 2011-02-23 kinaba: nm.incr(X, 1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } 4fd800b3a8 2011-02-23 kinaba: private: 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: void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { int Arg0 = 5; int Arg1 = 10; int Arg2 = 3; int Arg3 = 1; int Arg4 = 1; int Arg5 = 180; verify_case(0, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arg0 = 3; int Arg1 = 20; int Arg2 = 5; int Arg3 = 2; int Arg4 = 3; int Arg5 = 64; verify_case(1, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arg0 = 4; int Arg1 = 21; int Arg2 = 1; int Arg3 = 7; int Arg4 = 1; int Arg5 = 3087; verify_case(2, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 4; int Arg3 = 37; int Arg4 = 11; int Arg5 = 591860767; verify_case(3, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_4() { int Arg0 = 5; int Arg1 = 200000; int Arg2 = 999999999; int Arg3 = 123456789; int Arg4 = 987654321; int Arg5 = 499739175; verify_case(4, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { ProductOfPrices().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE