Artifact Content
Not logged in

Artifact 691135ca23ec3f165bf92a128f052aa061add56c


#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 SumsOfPerfectPowers { public:
	int howMany(int lowerBound, int upperBound) 
	{
		set<LL> sq;
		sq.insert(0);
		sq.insert(1);
		for(LL n=2; n*n<=upperBound; ++n)
			for(LL nk=n*n; nk<=upperBound; nk*=n)
				sq.insert(nk);
		vector<LL> sqv( sq.begin(), sq.end() );

		vector<LL> pp;
		for(vector<LL>::iterator it=sqv.begin(); it!=sqv.end(); ++it)
			for(vector<LL>::iterator jt=it; jt!=sqv.end() && *it+*jt<=upperBound; ++jt)
				pp.push_back( *it + *jt );
		sort( pp.begin(), pp.end() );
		pp.erase( unique(pp.begin(),pp.end()), pp.end() );

		vector<LL>::iterator s = lower_bound( pp.begin(), pp.end(), lowerBound );
		vector<LL>::iterator e = upper_bound( pp.begin(), pp.end(), upperBound );
		return distance(s, e);
	}
};

// 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(_, SumsOfPerfectPowers().howMany(lowerBound, upperBound));}
int main(){

CASE(0)
	int lowerBound = 0; 
	int upperBound = 1; 
	int _ = 2; 
END
CASE(1)
	int lowerBound = 5; 
	int upperBound = 6; 
	int _ = 1; 
END
CASE(2)
	int lowerBound = 25; 
	int upperBound = 30; 
	int _ = 5; 
END
CASE(3)
	int lowerBound = 103; 
	int upperBound = 103; 
	int _ = 0; 
END
CASE(4)
	int lowerBound = 1; 
	int upperBound = 100000; 
	int _ = 33604; 
END
CASE(5)
	int lowerBound = 0; 
	int upperBound = 0; 
	int _ = 1; 
END
CASE(6)
	int lowerBound = 0; 
	int upperBound = 5000000; 
	int _ = 1272868; 
END

}
// END CUT HERE