LINK
You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
Output
For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.
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组数据,每组有m,k,求出m的k次方的前三位数和后三位数
分析:
前三位:任意的数都可以写成10^x的形式 y=10^x log(10)y=x
结合题意:nk=10y 两边取对数 log(10)n^k=klog(10)n=y
如果n^k%10不等于0,则y就是浮点数,其中整数部分是10的多少次方,
小数部分是具体的数值,也就是科学计数法的形式。
本题求的是小数的前三位,设a是y的整数部分,b是y的小数部分;
0<y<1, 1<10^y<10,所以求前三次就100;
后三位:快速幂,记住模板,方法详见:快速幂
AC代码:
#include<stdio.h>
#include<math.h>
int main()
{
long long t,g,n,k,min,ans;
scanf("%lld",&t);
g=1;
while(t--)
{
ans=1;
scanf("%lld %lld",&n,&k);
double y=k*log10(n);
int min=y,p;
p=pow(10,y-min)*100;
while(k)
{
if(k%2!=0)
ans=ans*n%1000;
n=n*n%1000;
k/=2;
}
printf("Case %lld: %03d %lld\n",g++,p,ans);
//printf("%d ",pow(10,y-min)*100);这样写不行,输出结果不对)
}
}