Artifact Content
Not logged in

Artifact cbbb9cbac02277e48a3bd3b510b658a534e9f7ff


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

class HolyNumbers { public:
	vector<LL> erat(int N)
	{
		vector<LL> ps;
		vector<bool> is_prime(N+1, true);
		for(int p=2; p<=N; ++p)
			if(is_prime[p]) {
				ps.push_back(p);
				for(int q=p+p; q<=N; q+=p)
					is_prime[q] = false;
			}
		return ps;
	}

	long long count(long long upTo, int maximalPrime)
	{
		vector<LL> ps = erat(maximalPrime);
		return naive(ps, 0, upTo);
	}

	LL naive(const vector<LL>& ps, int i, LL upto)
	{
		if( i==ps.size() || upto<ps[i] )
			return 1;

		if( upto < ps[i]*ps[i] )
		{
			int k = upper_bound(ps.begin()+i, ps.end(), upto) - ps.begin();
			return (k-i)+1;
		}
		LL sum = naive(ps, i+1, upto);
		for(LL p=ps[i], pk=p; pk<=upto; pk*=p*p)
			sum += naive(ps, i+1, upto/pk);
		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(_, HolyNumbers().count(upTo, maximalPrime));}
int main(){

CASE(0)
	long long upTo = 10LL; 
	int maximalPrime = 100; 
	long long _ = 8LL; 
END
CASE(1)
	long long upTo = 10LL; 
	int maximalPrime = 3; 
	long long _ = 5LL; 
END
CASE(2)
	long long upTo = 123LL; 
	int maximalPrime = 12; 
	long long _ = 32LL; 
END
CASE(3)
	long long upTo = 123LL; 
	int maximalPrime = 456; 
	long long _ = 88LL; 
END
CASE(4)
	long long upTo = 123456789LL; 
	int maximalPrime = 12345; 
	long long _ = 25994500LL; 
END
CASE(5)
	long long upTo = 10000000000LL; 
	int maximalPrime = 1000000; 
	long long _ = -1LL; 
END
CASE(6)
	long long upTo = 10000000000LL; 
	int maximalPrime = 100; 
	long long _ = -1LL; 
END
}
// END CUT HERE