Artifact Content
Not logged in

Artifact 5a2518f603d0267494a3c5cc039bc0c448f796e6


#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 ThePowers { public:
	long long find(int A, int B)
	{
		LL total = 1;

		int prev_cnt = 1;
		for(int K=31; K>=1; --K)
		{
			int ub = invpow(A,K);
			int cnt = num_ppprime_under(ub);
			LL x = solve2(K,B) * (cnt - prev_cnt);
			total += x;
			prev_cnt = cnt;
		}
		return total;
	}

	// max x s.t. x^K <= A;
	int invpow(int A, int K)
	{
		int v = 0;
		int c = (int)pow(A, 1.0/K);
		for(int x=max(1,c-3); x<=c+3; ++x)
			if( pow_leq(x, K, A) )
				v = x;
		return v;
	}

	// number of non-square, cubic, ... numbers in [1,N]
	int num_ppprime_under(int N)
	{
		int total = N;
		vector<int> num(32);
		for(int p=num.size()-1; p>=2; --p)
		{
			num[p] = invpow(N, p) - 1;
			for(int q=p+p; q<num.size(); q+=p)
				num[p] -= num[q];
			total -= num[p];
		}
		return total;
	}

	int pow_leq(int x, int p, int N)
	{
		LL v = 1;
		for(int i=0; i<p; ++i) {
			v *= x;
			if(v>N)
				return false;
		}
		return true;
	}

	// number of [1,K]*[1,B]
	LL solve2(int K, int B)
	{
		set<LL> test;
		for(LL x=1; x<=K; ++x)
		for(LL y=1; y<=B; ++y)
			test.insert(x*y);
		return test.size();
	}
};

// 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(_, ThePowers().find(A, B));}
int main(){

CASE(0)
	int A = 7; 
	int B = 4; 
	long long _ = 23LL; 
END
CASE(1)
	int A = 1; 
	int B = 1; 
	long long _ = 1LL; 
END
CASE(2)
	int A = 1000000000; 
	int B = 1000000000; 
	long long _ = 999983644283653287LL; 
END
CASE(3)
	int A = 999999999; 
	int B = 5; 
	long long _ = 4999934406LL; 
END
/*
CASE(4)
	int A = ; 
	int B = ; 
	long long _ = LL; 
END
CASE(5)
	int A = ; 
	int B = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE