4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class StrongPrimePower 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <int> baseAndExponent(string n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL N; 4fd800b3a8 2011-02-23 kinaba: stringstream(n) >> N; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL sN = (LL) sqrt( (double)N ); 4fd800b3a8 2011-02-23 kinaba: if( sN*sN == N && isPrime(sN) ) { 4fd800b3a8 2011-02-23 kinaba: vector<int> ans; 4fd800b3a8 2011-02-23 kinaba: ans.push_back(sN); 4fd800b3a8 2011-02-23 kinaba: ans.push_back(2); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( (sN+1)*(sN+1) == N && isPrime(sN+1) ) { 4fd800b3a8 2011-02-23 kinaba: vector<int> ans; 4fd800b3a8 2011-02-23 kinaba: ans.push_back(sN+1); 4fd800b3a8 2011-02-23 kinaba: ans.push_back(2); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(LL p=2; p*p*p<=N; ++p) 4fd800b3a8 2011-02-23 kinaba: if( N%p==0 ) { 4fd800b3a8 2011-02-23 kinaba: LL q=0; 4fd800b3a8 2011-02-23 kinaba: while(N%p==0) 4fd800b3a8 2011-02-23 kinaba: N /= p, ++q; 4fd800b3a8 2011-02-23 kinaba: if( N==1 ) { 4fd800b3a8 2011-02-23 kinaba: vector<int> ans; 4fd800b3a8 2011-02-23 kinaba: ans.push_back(p); 4fd800b3a8 2011-02-23 kinaba: ans.push_back(q); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return vector<int>(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool isPrime(LL N) { 4fd800b3a8 2011-02-23 kinaba: for(LL p=2; p*p<=N; ++p) 4fd800b3a8 2011-02-23 kinaba: if(N%p==0) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ {}; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string n = "27"; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {3, 3 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string n = "10"; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string n = "7"; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string n = "1296"; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string n = "576460752303423488"; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {2, 59 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string n = "999999874000003969"; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {999999937, 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: