10f7b8dbe6 2015-06-11 kinaba: #include <iostream> 10f7b8dbe6 2015-06-11 kinaba: #include <sstream> 10f7b8dbe6 2015-06-11 kinaba: #include <iomanip> 10f7b8dbe6 2015-06-11 kinaba: #include <vector> 10f7b8dbe6 2015-06-11 kinaba: #include <string> 10f7b8dbe6 2015-06-11 kinaba: #include <map> 10f7b8dbe6 2015-06-11 kinaba: #include <set> 10f7b8dbe6 2015-06-11 kinaba: #include <algorithm> 10f7b8dbe6 2015-06-11 kinaba: #include <numeric> 10f7b8dbe6 2015-06-11 kinaba: #include <iterator> 10f7b8dbe6 2015-06-11 kinaba: #include <functional> 10f7b8dbe6 2015-06-11 kinaba: #include <complex> 10f7b8dbe6 2015-06-11 kinaba: #include <queue> 10f7b8dbe6 2015-06-11 kinaba: #include <stack> 10f7b8dbe6 2015-06-11 kinaba: #include <cmath> 10f7b8dbe6 2015-06-11 kinaba: #include <cassert> 10f7b8dbe6 2015-06-11 kinaba: #include <tuple> 10f7b8dbe6 2015-06-11 kinaba: using namespace std; 10f7b8dbe6 2015-06-11 kinaba: typedef long long LL; 10f7b8dbe6 2015-06-11 kinaba: typedef complex<double> CMP; 10f7b8dbe6 2015-06-11 kinaba: 10f7b8dbe6 2015-06-11 kinaba: class ModModMod { public: 10f7b8dbe6 2015-06-11 kinaba: long long findSum(vector <int> m, int R) 10f7b8dbe6 2015-06-11 kinaba: { 10f7b8dbe6 2015-06-11 kinaba: memo.assign(m.size()+1, -1); 10f7b8dbe6 2015-06-11 kinaba: return rec(m, 0, R); 10f7b8dbe6 2015-06-11 kinaba: } 10f7b8dbe6 2015-06-11 kinaba: 10f7b8dbe6 2015-06-11 kinaba: LL rec(const vector<int>& m, int i, int R) 10f7b8dbe6 2015-06-11 kinaba: { 10f7b8dbe6 2015-06-11 kinaba: if(i == m.size()) 10f7b8dbe6 2015-06-11 kinaba: return LL(1+R)*R/2; 10f7b8dbe6 2015-06-11 kinaba: return rec_memod(m, i)*(R/m[i]) + rec(m, i+1, R%m[i]); 10f7b8dbe6 2015-06-11 kinaba: } 10f7b8dbe6 2015-06-11 kinaba: 10f7b8dbe6 2015-06-11 kinaba: vector<LL> memo; 10f7b8dbe6 2015-06-11 kinaba: LL rec_memod(const vector<int>& m, int i) { 10f7b8dbe6 2015-06-11 kinaba: if(memo[i] >= 0) 10f7b8dbe6 2015-06-11 kinaba: return memo[i]; 10f7b8dbe6 2015-06-11 kinaba: return memo[i] = rec(m, i+1, m[i]-1); 10f7b8dbe6 2015-06-11 kinaba: } 10f7b8dbe6 2015-06-11 kinaba: }; 10f7b8dbe6 2015-06-11 kinaba: 10f7b8dbe6 2015-06-11 kinaba: // BEGIN CUT HERE 10f7b8dbe6 2015-06-11 kinaba: #include <ctime> 10f7b8dbe6 2015-06-11 kinaba: double start_time; string timer() 10f7b8dbe6 2015-06-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 10f7b8dbe6 2015-06-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 10f7b8dbe6 2015-06-11 kinaba: { os << "{ "; 10f7b8dbe6 2015-06-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 10f7b8dbe6 2015-06-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 10f7b8dbe6 2015-06-11 kinaba: void verify_case(const long long& Expected, const long long& Received) { 10f7b8dbe6 2015-06-11 kinaba: bool ok = (Expected == Received); 10f7b8dbe6 2015-06-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 10f7b8dbe6 2015-06-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 10f7b8dbe6 2015-06-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 10f7b8dbe6 2015-06-11 kinaba: #define END verify_case(_, ModModMod().findSum(m, R));} 10f7b8dbe6 2015-06-11 kinaba: int main(){ 10f7b8dbe6 2015-06-11 kinaba: 10f7b8dbe6 2015-06-11 kinaba: CASE(0) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = {5,3,2}; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = 10; 10f7b8dbe6 2015-06-11 kinaba: long long _ = 4LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: CASE(1) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = {33, 15, 7, 10, 100, 9, 5}; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = 64; 10f7b8dbe6 2015-06-11 kinaba: long long _ = 92LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: CASE(2) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = {995,149,28,265,275,107,555,241,702,462,519,212,362,478,783,381,602,546,183,886,59,317,977,612,328,91,771,131}; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = 992363; 10f7b8dbe6 2015-06-11 kinaba: long long _ = 12792005LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: CASE(3) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10f7b8dbe6 2015-06-11 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = 100; 10f7b8dbe6 2015-06-11 kinaba: long long _ = 4950LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: CASE(4) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = {2934}; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = 10000000; 10f7b8dbe6 2015-06-11 kinaba: long long _ = 14664070144LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: /* 10f7b8dbe6 2015-06-11 kinaba: CASE(5) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = ; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = ; 10f7b8dbe6 2015-06-11 kinaba: long long _ = LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: CASE(6) 10f7b8dbe6 2015-06-11 kinaba: int m_[] = ; 10f7b8dbe6 2015-06-11 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); 10f7b8dbe6 2015-06-11 kinaba: int R = ; 10f7b8dbe6 2015-06-11 kinaba: long long _ = LL; 10f7b8dbe6 2015-06-11 kinaba: END 10f7b8dbe6 2015-06-11 kinaba: */ 10f7b8dbe6 2015-06-11 kinaba: } 10f7b8dbe6 2015-06-11 kinaba: // END CUT HERE