Artifact Content
Not logged in

Artifact 4a490e7a696723ed040a3e8658b617b79ff6b1cf


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class SieveOfEratosthenes { public:
	vector<int> eratos(int M)
	{
		vector<bool> isComposite(M+1);
		vector<int> ps;
		for(int p=2; p<=M; ++p)
			if( !isComposite[p] )
			{
				ps.push_back(p);
				for(LL q=LL(p)*p; q<=M; q+=p)
					isComposite[q] = true;
			}
		return ps;
	}

	int minFactor(int N, const vector<int>& ps)
	{
		for(int i=0; i<ps.size(); ++i)
			if( N % ps[i] == 0 )
				return ps[i];
		return INT_MAX;
	}

	int lastScratch(int maxNum)
	{
		vector<int> ps = eratos(int(sqrt(double(maxNum))));
		int P = ps.back();
		int M = P*P;
		for(int Q=P+1; P*Q<=maxNum; ++Q)
			if( minFactor(Q,ps) >= P )
				M = P*Q;
		return M;
	}
};

// 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 int& Expected, const int& 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(_, SieveOfEratosthenes().lastScratch(maxNum));}
int main(){

CASE(0)
	int maxNum = 18; 
	int _ = 15; 
END
CASE(1)
	int maxNum = 5; 
	int _ = 4; 
END
CASE(2)
	int maxNum = 100; 
	int _ = 91; 
END
CASE(3)
	int maxNum = 400; 
	int _ = 361; 
END
CASE(4)
	int maxNum = 2000000000; 
	int _ = -1; 
END
CASE(5)
	int maxNum = 8; 
	int _ = 8; 
END
}
// END CUT HERE