Artifact Content
Not logged in

Artifact d6a6dfb9fe6aa739dad03506d418e42950195d9b


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class SumAndProductPuzzle { public:
	long long getSum(int A, int B)
	{
		static const int N = 5000000;
		vector<bool> isp(N+1, true);
		for(int p=2; p<=N; ++p)
			if(isp[p])
				for(int q=p+p; q<=N; q+=p)
					isp[q] = false;

		vector<bool> has_dec(N+1, false);
		for(int p=2; p<=N; ++p)
			for(int S=p+p; S<=N; S+=p) {
				int q = S/p;
				if(!isp[p+q-1])
					has_dec[S] = true;
			}

		LL sum = 0;
		for(int S=A; S<=B; ++S)
			if(!isp[S-1] && !has_dec[S-1])
				sum += S;
		return sum;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SumAndProductPuzzle().getSum(A, B));}
int main(){

CASE(0)
	int A = 30; 
	int B = 33; 
	long long _ = 33LL; 
END
CASE(1)
	int A = 8; 
	int B = 11; 
	long long _ = 19LL; 
END
CASE(2)
	int A = 40; 
	int B = 43; 
	long long _ = 0LL; 
END
CASE(3)
	int A = 47; 
	int B = 74; 
	long long _ = 168LL; 
END
CASE(4)
	int A = 1; 
	int B = 2; 
	long long _ = 0LL; 
END
CASE(5)
	int A = 1; 
	int B = 5000000; 
	long long _ = -1LL; 
END
CASE(6)
	int A = 1; 
	int B = 1; 
	long long _ = 0LL; 
END
}
// END CUT HERE