LightOJ - 1282
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 < 2^31)and k (1 ≤ k ≤ 10^7).
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.
题目大意
求 n 的 k 次方结果的前三位数 和 后三位数
快速幂的话可以单独去学一下,我记得当时我是通过位运算的思想去理解的
Sample Input
5
123456 1
123456 2
2 31
2 32
29 8751919
Sample Output
5
123456 1
123456 2
2 31
2 32
29 8751919
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int t,z=0,s;
scanf("%d",&t);
long long n,k;
while(t--)
{
s=1;
z++;
scanf("%lld %lld",&n,&k);
long long k1=k,m=n;
while(k1)//快速幂
{
if(k1&1)// 判断奇偶 // 3 0 0 1 1
//& 1 0 0 0 1
// 0 1
s=s*m%1000;
m=m*m%1000;
k1/=2;
}
// 转化成类似科学计数法
//123456 = 1.23456 x 10^5 = 10^n x 10^5 (n为1.23456的常用对数)
double p=k*log10(n);
long long g=p; //把 p 的整数位给 g
int h=pow(10,p-g)*100; //取小数位
printf("Case %d: ",z);
printf("%d %03d\n",h,s);
}
return 0;
}