d3b1273780 2014-06-12 kinaba: #include <iostream> d3b1273780 2014-06-12 kinaba: #include <sstream> d3b1273780 2014-06-12 kinaba: #include <iomanip> d3b1273780 2014-06-12 kinaba: #include <vector> d3b1273780 2014-06-12 kinaba: #include <string> d3b1273780 2014-06-12 kinaba: #include <map> d3b1273780 2014-06-12 kinaba: #include <set> d3b1273780 2014-06-12 kinaba: #include <algorithm> d3b1273780 2014-06-12 kinaba: #include <numeric> d3b1273780 2014-06-12 kinaba: #include <iterator> d3b1273780 2014-06-12 kinaba: #include <functional> d3b1273780 2014-06-12 kinaba: #include <complex> d3b1273780 2014-06-12 kinaba: #include <queue> d3b1273780 2014-06-12 kinaba: #include <stack> d3b1273780 2014-06-12 kinaba: #include <cmath> d3b1273780 2014-06-12 kinaba: #include <cassert> d3b1273780 2014-06-12 kinaba: #include <tuple> d3b1273780 2014-06-12 kinaba: using namespace std; d3b1273780 2014-06-12 kinaba: typedef long long LL; d3b1273780 2014-06-12 kinaba: typedef complex<double> CMP; d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: class SumAndProductPuzzle { public: d3b1273780 2014-06-12 kinaba: long long getSum(int A, int B) d3b1273780 2014-06-12 kinaba: { d3b1273780 2014-06-12 kinaba: static const int N = 5000000; d3b1273780 2014-06-12 kinaba: vector<bool> isp(N+1, true); d3b1273780 2014-06-12 kinaba: for(int p=2; p<=N; ++p) d3b1273780 2014-06-12 kinaba: if(isp[p]) d3b1273780 2014-06-12 kinaba: for(int q=p+p; q<=N; q+=p) d3b1273780 2014-06-12 kinaba: isp[q] = false; d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: vector<bool> has_dec(N+1, false); d3b1273780 2014-06-12 kinaba: for(int p=2; p<=N; ++p) d3b1273780 2014-06-12 kinaba: for(int S=p+p; S<=N; S+=p) { d3b1273780 2014-06-12 kinaba: int q = S/p; d3b1273780 2014-06-12 kinaba: if(!isp[p+q-1]) d3b1273780 2014-06-12 kinaba: has_dec[S] = true; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: LL sum = 0; d3b1273780 2014-06-12 kinaba: for(int S=A; S<=B; ++S) d3b1273780 2014-06-12 kinaba: if(!isp[S-1] && !has_dec[S-1]) d3b1273780 2014-06-12 kinaba: sum += S; d3b1273780 2014-06-12 kinaba: return sum; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: }; d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: // BEGIN CUT HERE d3b1273780 2014-06-12 kinaba: #include <ctime> d3b1273780 2014-06-12 kinaba: double start_time; string timer() d3b1273780 2014-06-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d3b1273780 2014-06-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d3b1273780 2014-06-12 kinaba: { os << "{ "; d3b1273780 2014-06-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d3b1273780 2014-06-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d3b1273780 2014-06-12 kinaba: void verify_case(const long long& Expected, const long long& Received) { d3b1273780 2014-06-12 kinaba: bool ok = (Expected == Received); d3b1273780 2014-06-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d3b1273780 2014-06-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d3b1273780 2014-06-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d3b1273780 2014-06-12 kinaba: #define END verify_case(_, SumAndProductPuzzle().getSum(A, B));} d3b1273780 2014-06-12 kinaba: int main(){ d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: CASE(0) d3b1273780 2014-06-12 kinaba: int A = 30; d3b1273780 2014-06-12 kinaba: int B = 33; d3b1273780 2014-06-12 kinaba: long long _ = 33LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(1) d3b1273780 2014-06-12 kinaba: int A = 8; d3b1273780 2014-06-12 kinaba: int B = 11; d3b1273780 2014-06-12 kinaba: long long _ = 19LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(2) d3b1273780 2014-06-12 kinaba: int A = 40; d3b1273780 2014-06-12 kinaba: int B = 43; d3b1273780 2014-06-12 kinaba: long long _ = 0LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(3) d3b1273780 2014-06-12 kinaba: int A = 47; d3b1273780 2014-06-12 kinaba: int B = 74; d3b1273780 2014-06-12 kinaba: long long _ = 168LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(4) d3b1273780 2014-06-12 kinaba: int A = 1; d3b1273780 2014-06-12 kinaba: int B = 2; d3b1273780 2014-06-12 kinaba: long long _ = 0LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(5) d3b1273780 2014-06-12 kinaba: int A = 1; d3b1273780 2014-06-12 kinaba: int B = 5000000; d3b1273780 2014-06-12 kinaba: long long _ = -1LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(6) d3b1273780 2014-06-12 kinaba: int A = 1; d3b1273780 2014-06-12 kinaba: int B = 1; d3b1273780 2014-06-12 kinaba: long long _ = 0LL; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: // END CUT HERE