Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double datatype format, but you won’t get all the digits of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.
Input
The first line of input will be an intege.T<1001, where T represents the number of test cases. Each of the next T lines contains two positive integers,n and k.n will fit in
32 bit integer and k will be less than 10000001.
Output
For each line of input there will be one line of output. It will be of the formt.LLL:::TTT, where LLL represents the first three digits of n,k and TTT represents the last three digits of n,k. You are assured that n k will contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123...456
152...936
题目大意:两个数n、k,求n^k的前三位数字与后三位数字。
题目解析:后三位数字很容易求,通过幂取模即可。先考虑n不做幂运算时如何取前三位,令d=log10(n),则d=a(整数部分)+i(小数部分),n=10^d=10^(a+i)=10^a*10^i。
则10^i*100即为前三位。以123456为例,d=log10(123456)=5+i,则123456=10^i*10^5。又因为123456=1.23456*10^5,所以10^i=1.23456,所以前三位为10^i*100=123。
再来考虑n做幂运算时,将n^k变换为10^(klog(n)),则d=k*log10(n),接下来就不必细说了。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<string>
# include<cmath>
# include<algorithm>
using namespace std;
int mypow(long long a,long long b)
{
if(b==0)
return 1;
if(b==1){
a%=1000;
return a;
}
long long t=mypow(a,b/2);
t*=t;
t%=1000;
if(b&1)
t*=a;
t%=1000;
return t;
}
int main()
{
int T,k,i;
long long n;
scanf("%d",&T);
while(T--)
{
cin>>n>>k;
int ans2=mypow(n,k);
double u=k*log10(n);
double ans1=pow(10,u-(int)u)*100;
printf("%d...%03d\n",(int)ans1,ans2);
}
return 0;
}