题干:
给你两个数n,k,求
n
k
n^k
nk 的前三位和后三位。
n (2 ≤ n <
2
31
2^{31}
231) and k (1 ≤ k ≤
1
0
7
10^7
107).
思路:
后三位比较好求,就是再对
n
k
n^k
nk 求快速幂的时候模1000就行;
对于前三位,我们先设
a
b
a^{b}
ab=
1
0
c
10^{c}
10c ==>
log
10
a
b
\log_{10} a^{b}
log10ab=
log
10
1
0
c
\log_{10} 10^{c}
log1010c ==> b*
log
10
a
\log_{10} a
log10a = c
1
0
c
10^{c}
10c =
1
0
c
1
+
c
2
10^{c1+c2}
10c1+c2 =
1
0
c
1
10^{c1}
10c1 *
1
0
c
2
10^{c2}
10c2(c1为c的整数部分,c2为c的小数部分)
所以
1
0
c
1
10^{c1}
10c1 代表后缀零,
1
0
c
2
10^{c2}
10c2前几位数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const long long c=1000;
long long ol(long long n)
{
long long ans=n;
for(int i=2;i*i<=n;i++){
if(n%i==0)
{
ans-=ans/i;
while(n%i==0)
n/=i;
}
}
if(n>1) ans-=ans/n;
return ans;
}
long long qc(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%c;
a=(a*a)%c;
b>>=1;
//printf("%lld %lld %lld\n",a,b,ans);
}
return ans;
}
int main()
{
long long t,a,b,ans;
double p,ans1;
scanf("%d",&t);
//printf("%lld\n",oc);
for(int i=1;i<=t;i++){
scanf("%lld%lld",&a,&b);
p=(double)b*log10(a*1.0);
p-=(int)p;
//printf("%lf\n",p);
ans1=pow(10.0,p);
ans=qc(a,b);
printf("Case %d: %d %03d\n",i,(int)(ans1*100),ans);
}
return 0;
}