Advertisement
Guest User

TC SRM 400(500)

a guest
Jun 9th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19.  
  20. using namespace std;
  21.  
  22. class StrongPrimePower {
  23. public:
  24.     vector <int> baseAndExponent(string);
  25. };
  26.  
  27. typedef long long ll;
  28. typedef long double ld;
  29. const ld eps = 1e-14;
  30. ld logbase(ld a, ld base)
  31. {
  32.    return log(a) / log(base);
  33. }
  34.  
  35. bool prime(int a){
  36.     for( int i = 2; i < sqrt(double(a))+1 && i<a; i++ )
  37.         if( a%i==0 )
  38.             return false;
  39.     return true;
  40. }
  41. double round(double a){
  42.     double fract = a - floor(a);
  43.     if( fract<0.5 )
  44.         return floor(a);
  45.     else
  46.         return ceil(a);
  47. }
  48.  
  49. vector <int> StrongPrimePower::baseAndExponent(string n) {
  50.     int len = n.length();
  51.     ll N = 0;
  52.     for( int i = 0; i < len; i++ ){
  53.         N*=10;
  54.         N+=n[i]-'0';
  55.     }
  56.     for( int i = 2; i < logbase( ld(N),2 )+1; i++ ){
  57.         ld num = pow( ld(N), 1/ld(i) );
  58.        
  59.         if( abs( round(num)-num )  <= eps && prime( int( round(num) ) ) ){
  60.             vector<int> ret;
  61.             ret.push_back( int(round(num)) );
  62.             ret.push_back( i );
  63.             return ret;
  64.         }
  65.     }
  66.     return vector<int>();
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement