e5fc5dffb3 2015-07-23 kinaba: #include <iostream> e5fc5dffb3 2015-07-23 kinaba: #include <sstream> e5fc5dffb3 2015-07-23 kinaba: #include <iomanip> e5fc5dffb3 2015-07-23 kinaba: #include <vector> e5fc5dffb3 2015-07-23 kinaba: #include <string> e5fc5dffb3 2015-07-23 kinaba: #include <map> e5fc5dffb3 2015-07-23 kinaba: #include <set> e5fc5dffb3 2015-07-23 kinaba: #include <algorithm> e5fc5dffb3 2015-07-23 kinaba: #include <numeric> e5fc5dffb3 2015-07-23 kinaba: #include <iterator> e5fc5dffb3 2015-07-23 kinaba: #include <functional> e5fc5dffb3 2015-07-23 kinaba: #include <complex> e5fc5dffb3 2015-07-23 kinaba: #include <queue> e5fc5dffb3 2015-07-23 kinaba: #include <stack> e5fc5dffb3 2015-07-23 kinaba: #include <cmath> e5fc5dffb3 2015-07-23 kinaba: #include <cassert> e5fc5dffb3 2015-07-23 kinaba: #include <tuple> e5fc5dffb3 2015-07-23 kinaba: using namespace std; e5fc5dffb3 2015-07-23 kinaba: typedef long long LL; e5fc5dffb3 2015-07-23 kinaba: typedef complex<double> CMP; e5fc5dffb3 2015-07-23 kinaba: e5fc5dffb3 2015-07-23 kinaba: class ModModMod { public: e5fc5dffb3 2015-07-23 kinaba: long long findSum(vector <int> m, int R) e5fc5dffb3 2015-07-23 kinaba: { e5fc5dffb3 2015-07-23 kinaba: memo.assign(m.size()+1, -1); e5fc5dffb3 2015-07-23 kinaba: return rec(m, 0, R); e5fc5dffb3 2015-07-23 kinaba: } e5fc5dffb3 2015-07-23 kinaba: e5fc5dffb3 2015-07-23 kinaba: LL rec(const vector<int>& m, int i, int R) e5fc5dffb3 2015-07-23 kinaba: { e5fc5dffb3 2015-07-23 kinaba: if(i == m.size()) e5fc5dffb3 2015-07-23 kinaba: return LL(1+R)*R/2; e5fc5dffb3 2015-07-23 kinaba: return rec_memod(m, i)*(R/m[i]) + rec(m, i+1, R%m[i]); e5fc5dffb3 2015-07-23 kinaba: } e5fc5dffb3 2015-07-23 kinaba: e5fc5dffb3 2015-07-23 kinaba: vector<LL> memo; e5fc5dffb3 2015-07-23 kinaba: LL rec_memod(const vector<int>& m, int i) { e5fc5dffb3 2015-07-23 kinaba: if(memo[i] >= 0) e5fc5dffb3 2015-07-23 kinaba: return memo[i]; e5fc5dffb3 2015-07-23 kinaba: return memo[i] = rec(m, i+1, m[i]-1); e5fc5dffb3 2015-07-23 kinaba: } e5fc5dffb3 2015-07-23 kinaba: }; e5fc5dffb3 2015-07-23 kinaba: e5fc5dffb3 2015-07-23 kinaba: // BEGIN CUT HERE e5fc5dffb3 2015-07-23 kinaba: #include <ctime> e5fc5dffb3 2015-07-23 kinaba: double start_time; string timer() e5fc5dffb3 2015-07-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } e5fc5dffb3 2015-07-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) e5fc5dffb3 2015-07-23 kinaba: { os << "{ "; e5fc5dffb3 2015-07-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) e5fc5dffb3 2015-07-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } e5fc5dffb3 2015-07-23 kinaba: void verify_case(const long long& Expected, const long long& Received) { e5fc5dffb3 2015-07-23 kinaba: bool ok = (Expected == Received); e5fc5dffb3 2015-07-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; e5fc5dffb3 2015-07-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } e5fc5dffb3 2015-07-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); e5fc5dffb3 2015-07-23 kinaba: #define END verify_case(_, ModModMod().findSum(m, R));} e5fc5dffb3 2015-07-23 kinaba: int main(){ e5fc5dffb3 2015-07-23 kinaba: e5fc5dffb3 2015-07-23 kinaba: CASE(0) e5fc5dffb3 2015-07-23 kinaba: int m_[] = {5,3,2}; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = 10; e5fc5dffb3 2015-07-23 kinaba: long long _ = 4LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: CASE(1) e5fc5dffb3 2015-07-23 kinaba: int m_[] = {33, 15, 7, 10, 100, 9, 5}; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = 64; e5fc5dffb3 2015-07-23 kinaba: long long _ = 92LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: CASE(2) e5fc5dffb3 2015-07-23 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}; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = 992363; e5fc5dffb3 2015-07-23 kinaba: long long _ = 12792005LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: CASE(3) e5fc5dffb3 2015-07-23 kinaba: int m_[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, e5fc5dffb3 2015-07-23 kinaba: 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = 100; e5fc5dffb3 2015-07-23 kinaba: long long _ = 4950LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: CASE(4) e5fc5dffb3 2015-07-23 kinaba: int m_[] = {2934}; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = 10000000; e5fc5dffb3 2015-07-23 kinaba: long long _ = 14664070144LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: /* e5fc5dffb3 2015-07-23 kinaba: CASE(5) e5fc5dffb3 2015-07-23 kinaba: int m_[] = ; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = ; e5fc5dffb3 2015-07-23 kinaba: long long _ = LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: CASE(6) e5fc5dffb3 2015-07-23 kinaba: int m_[] = ; e5fc5dffb3 2015-07-23 kinaba: vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_)); e5fc5dffb3 2015-07-23 kinaba: int R = ; e5fc5dffb3 2015-07-23 kinaba: long long _ = LL; e5fc5dffb3 2015-07-23 kinaba: END e5fc5dffb3 2015-07-23 kinaba: */ e5fc5dffb3 2015-07-23 kinaba: } e5fc5dffb3 2015-07-23 kinaba: // END CUT HERE