题目链接:https://cn.vjudge.net/contest/318269#problem/C
Sample Input
5
123456 1
123456 2
2 31
2 32
29 8751919
Sample Output
Case 1: 123 456
Case 2: 152 936
Case 3: 214 648
Case 4: 429 296
Case 5: 665 669
翻译:
给你两个数,n (2 ≤ n < 2^31) and k (1 ≤ k ≤ 10^7). 求n^k的前三位和后三位。
分析:对于后三位,用快速幂对1000取余,即可求得。
对于后三位:
先对n^k进行降幂处理。
根据对数的性质,n^k可以表示为 10^(x+y)次幂。
我们假设x为整数部分,y为小数部分,
x影响的只是小数点的位置,y影响的是每一位上的值。
y为小数,10^y次幂,大于1,(10 ^ 0=1).所以在乘以100,取整,就得到前三位的值。
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
#define mod 1000
LL mod_pow(LL n,LL k)/*快速幂求后三位*/
{
LL res=1;
while(k)
{
if(k&1)
res=res*n%mod;
n=n*n%mod;
k>>=1;
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
int f=1;
while(T--)
{
LL n,k;
scanf("%lld %lld",&n,&k);
printf("Case %d: ",f++);
LL behid=mod_pow(n,k);
double q=k*log10(n);/*对n^k降幂处理*/
LL first=pow(10,q-(int)q)*100;
printf("%lld ",first);
printf("%03lld\n",behid);
}
return 0;
}