低三位用2分法对1000取模即可
高三位先将n^m转为科学计数法p.q*10^t
log(10,n^m) = log(10,p.q*10^t),
m*log(10,n) = t+log(10,p.q);
m*log(10,n)的整数部分对应t,小数部分对应log(10,p.q),即可以求出p.q
将p.q的前三位有效数字取出即为高三位
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int getLow(int m,int n)
{
if(n==1)
return m%1000;
int temp = getLow(m,n/2);
if(n%2==0)
return temp*temp%1000;
else
return (temp*temp%1000*(m%1000))%1000;
}
int getHigh(int m,int n)
{
double temp = pow(10,n*log10(m)-int(n*log10(m)));
while(temp<100-1e-10)
{
temp *= 10;
}
return (int)temp;
}
int main()
{
int T,n,k;
scanf("%d",&T);
for(int i = 1;i<=T;i++)
{
scanf("%d %d",&n,&k);
int ans1 = getHigh(n,k);
int ans2 = getLow(n,k);
printf("Case %d: %3d %03d\n",i,ans1,ans2);
}
return 0;
}