Problem Description
输入两个数n, k问你n^k次方前三位数是多少,后三位数是多少。
思路:
后三位快速幂%1000就好了。
核心是求前三位 n^k = 10^(a + b) 其中a为整数,b为小数。前三位就是 10^b * 100。所以我们得求出b, log(10)n^k = log(10)10^(a+b) -> k*log(10)^n = a + b, 因为b是小数,b = (a+b)%1 = (k*log(10)^n)%1。前三位就知道是多少了
#include<bits/stdc++.h>
using namespace std;
int Pow(long long n, long long k, int mod)//快速幂
{
long long sum = 1, x = n;
while(k)
{
if(k&1) sum = (sum * x) % mod;
x = (x * x) % mod;
k >>= 1;
}
return sum;
}
int main()
{
int T, cas = 1;
long long n, k;
scanf("%d", &T);
while(T--)
{
scanf("%lld %lld", &n, &k);
int ans1 = pow(10, 2.0 + fmod(1.0*k*log10(n), 1.0));//10^b * 100
int ans2 = Pow(n, k, 1000);
printf("Case %d: %d %03d\n", cas++, ans1, ans2);
}
}