Leading and Trailing LightOJ - 1282

版权声明: https://blog.csdn.net/weixin_40959045/article/details/88382083

求n^k的前三位和后三位

  1. 后三位快速幂即可得到
  2. 对于前三位
    1. a.bc * 10^t = n^k
    2. log_{10}{a.bc} + t = klog_{10|{n}
    3. 所以log_{10}{a.bc} 是klog_{10|{n}的小数部分
    4. a.bc = 1000 * klog_{10|{n} 的整数部分
 #include<bits/stdc++.h>
 using namespace std;
 #define fst first
 #define sec second
 #define sci(num) scanf("%d",&num)
 #define scl(num) scanf("%lld",&num)
 #define mem(a,b) memset(a,b,sizeof a)
 #define cpy(a,b) memcopy(a,b,sizeof b)
 typedef long long LL;
 typedef pair<int,int> P;
 const int MAX_N = 510;
 const int MAX_M = 10000;
 int mpow(int a,int n,int mod) {
     int ans = 1;
     while (n) {
         if (n & 1) ans = ans * a % mod;
         a = a * a % mod;
         n >>= 1;
     }
     return ans;
 }
 int main() {
     int T;
     cin >> T;
     for (int cs =1; cs <= T;cs++) {
         LL N,K;
         cin >> N >> K;
         double x =  K* log10(N) -(LL) (K* log10(N));
         //cout <<  pow(10,x) << endl;
         LL ans1 = 100 * pow(10,x);
         LL ans2 =mpow(N % 1000,K,1000);
         cout << "Case " << cs << ": ";
         printf("%lld %03lld\n",ans1,ans2);
     }
     return 0;
 }

猜你喜欢

转载自blog.csdn.net/weixin_40959045/article/details/88382083
今日推荐