c009363094 2013-04-06 kinaba: #include <iostream> c009363094 2013-04-06 kinaba: #include <sstream> c009363094 2013-04-06 kinaba: #include <iomanip> c009363094 2013-04-06 kinaba: #include <vector> c009363094 2013-04-06 kinaba: #include <string> c009363094 2013-04-06 kinaba: #include <map> c009363094 2013-04-06 kinaba: #include <set> c009363094 2013-04-06 kinaba: #include <algorithm> c009363094 2013-04-06 kinaba: #include <numeric> c009363094 2013-04-06 kinaba: #include <iterator> c009363094 2013-04-06 kinaba: #include <functional> c009363094 2013-04-06 kinaba: #include <complex> c009363094 2013-04-06 kinaba: #include <queue> c009363094 2013-04-06 kinaba: #include <stack> c009363094 2013-04-06 kinaba: #include <cmath> c009363094 2013-04-06 kinaba: #include <cassert> c009363094 2013-04-06 kinaba: using namespace std; c009363094 2013-04-06 kinaba: typedef long long LL; c009363094 2013-04-06 kinaba: typedef long double LD; c009363094 2013-04-06 kinaba: typedef complex<LD> CMP; c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: class ThePowers { public: c009363094 2013-04-06 kinaba: long long find(int A, int B) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: LL total = 1; c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: int prev_cnt = 1; c009363094 2013-04-06 kinaba: for(int K=31; K>=1; --K) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: int ub = invpow(A,K); c009363094 2013-04-06 kinaba: int cnt = num_ppprime_under(ub); c009363094 2013-04-06 kinaba: LL x = solve2(K,B) * (cnt - prev_cnt); c009363094 2013-04-06 kinaba: total += x; c009363094 2013-04-06 kinaba: prev_cnt = cnt; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: return total; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: // max x s.t. x^K <= A; c009363094 2013-04-06 kinaba: int invpow(int A, int K) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: int v = 0; c009363094 2013-04-06 kinaba: int c = (int)pow(A, 1.0/K); c009363094 2013-04-06 kinaba: for(int x=max(1,c-3); x<=c+3; ++x) c009363094 2013-04-06 kinaba: if( pow_leq(x, K, A) ) c009363094 2013-04-06 kinaba: v = x; c009363094 2013-04-06 kinaba: return v; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: // number of non-square, cubic, ... numbers in [1,N] c009363094 2013-04-06 kinaba: int num_ppprime_under(int N) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: int total = N; c009363094 2013-04-06 kinaba: vector<int> num(32); c009363094 2013-04-06 kinaba: for(int p=num.size()-1; p>=2; --p) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: num[p] = invpow(N, p) - 1; c009363094 2013-04-06 kinaba: for(int q=p+p; q<num.size(); q+=p) c009363094 2013-04-06 kinaba: num[p] -= num[q]; c009363094 2013-04-06 kinaba: total -= num[p]; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: return total; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: int pow_leq(int x, int p, int N) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: LL v = 1; c009363094 2013-04-06 kinaba: for(int i=0; i<p; ++i) { c009363094 2013-04-06 kinaba: v *= x; c009363094 2013-04-06 kinaba: if(v>N) c009363094 2013-04-06 kinaba: return false; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: return true; c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: // number of [1,K]*[1,B] c009363094 2013-04-06 kinaba: LL solve2(int K, int B) c009363094 2013-04-06 kinaba: { c009363094 2013-04-06 kinaba: set<LL> test; c009363094 2013-04-06 kinaba: for(LL x=1; x<=K; ++x) c009363094 2013-04-06 kinaba: for(LL y=1; y<=B; ++y) c009363094 2013-04-06 kinaba: test.insert(x*y); c009363094 2013-04-06 kinaba: return test.size(); c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: }; c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: // BEGIN CUT HERE c009363094 2013-04-06 kinaba: #include <ctime> c009363094 2013-04-06 kinaba: double start_time; string timer() c009363094 2013-04-06 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c009363094 2013-04-06 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c009363094 2013-04-06 kinaba: { os << "{ "; c009363094 2013-04-06 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c009363094 2013-04-06 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c009363094 2013-04-06 kinaba: void verify_case(const long long& Expected, const long long& Received) { c009363094 2013-04-06 kinaba: bool ok = (Expected == Received); c009363094 2013-04-06 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c009363094 2013-04-06 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c009363094 2013-04-06 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c009363094 2013-04-06 kinaba: #define END verify_case(_, ThePowers().find(A, B));} c009363094 2013-04-06 kinaba: int main(){ c009363094 2013-04-06 kinaba: c009363094 2013-04-06 kinaba: CASE(0) c009363094 2013-04-06 kinaba: int A = 7; c009363094 2013-04-06 kinaba: int B = 4; c009363094 2013-04-06 kinaba: long long _ = 23LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: CASE(1) c009363094 2013-04-06 kinaba: int A = 1; c009363094 2013-04-06 kinaba: int B = 1; c009363094 2013-04-06 kinaba: long long _ = 1LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: CASE(2) c009363094 2013-04-06 kinaba: int A = 1000000000; c009363094 2013-04-06 kinaba: int B = 1000000000; c009363094 2013-04-06 kinaba: long long _ = 999983644283653287LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: CASE(3) c009363094 2013-04-06 kinaba: int A = 999999999; c009363094 2013-04-06 kinaba: int B = 5; c009363094 2013-04-06 kinaba: long long _ = 4999934406LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: /* c009363094 2013-04-06 kinaba: CASE(4) c009363094 2013-04-06 kinaba: int A = ; c009363094 2013-04-06 kinaba: int B = ; c009363094 2013-04-06 kinaba: long long _ = LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: CASE(5) c009363094 2013-04-06 kinaba: int A = ; c009363094 2013-04-06 kinaba: int B = ; c009363094 2013-04-06 kinaba: long long _ = LL; c009363094 2013-04-06 kinaba: END c009363094 2013-04-06 kinaba: */ c009363094 2013-04-06 kinaba: } c009363094 2013-04-06 kinaba: // END CUT HERE