iscsi

SRM 437 D2 1000 ThePower

Jul 28th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <ctime>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <cctype>
  13. #include <cmath>
  14. using namespace std;
  15. typedef long long LL;
  16. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  17. #define REP(k,a) for(int k=0; k < (a); ++k)
  18.  
  19. class ThePower {
  20.     int cacheS;
  21.     vector<LL> cache;
  22.     LL INF;
  23.     LL mSqrt(LL a, double r)
  24.     {
  25.         return (LL) pow((double) a,1.0/r );
  26.     }
  27.     LL mPow(LL a, int i)
  28.     {
  29.         if(i==1) return a;
  30.         LL tmp=a*mPow(a,i-1);
  31.         return (tmp>a)?tmp:INF;
  32.     }
  33.     int calc(LL n)
  34.     {
  35.         if(n<cacheS && cache[n]!=-1)
  36.             return cache[n];
  37.         LL res=n-1;
  38.         FOR(i,2,70)
  39.         {
  40.             LL tmp=mSqrt(n,i);
  41.             FOR(j,-50,50)
  42.                 if(tmp+j>0)
  43.                 {
  44.                     LL ttmp=mPow(tmp+j,i);
  45.                     if (ttmp==INF)
  46.                         continue;
  47.                     ttmp-=n;
  48.                     if(ttmp<0)
  49.                         ttmp*=-1;
  50.                     if(res>1+ttmp)     
  51.                         res=min(res,1+ttmp+calc(tmp+j));
  52.                 }
  53.         }
  54.         if(n<cacheS)
  55.             cache[n]=res;
  56.         return res;
  57.     }
  58. public:
  59.  
  60.     int count(LL n)
  61.     {
  62.         INF=1000000000000LL;
  63.         cacheS=1000000;
  64.         cache.resize(cacheS,-1);
  65.         return calc(n);
  66.     }
  67. };
Advertisement
Add Comment
Please, Sign In to add comment