给定两个数n,k 求n^k的前三位和最后三位
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
后三位是知道靠快速幂取模来求出,前三位是靠着数学中的log10的关系来找的,这里放大佬写的知识点的链接https://blog.csdn.net/gscsdlz/article/details/51886094
还要注意因为后三位中会出现00X、0XX等,所以最好用%03d、%03lld
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int mod=1000;
ll mode(ll a,ll b){
ll sum=1;
a=a%mod;
while(b){
if(b&1)
sum=(sum*a)%mod;
b/=2;
a=(a*a)%mod;
}
return sum;
}//快速幂
ll t,n,m;
int main()
{
cin>>t;
int cas=0;
while(t--){
cin>>n>>m;
ll sum=mode(n,m);//直接将mod设为1000取后三位
double ans=fmod((double)m * log10((double)n), 1);//取log10(n^k)小数部分
int res = pow(10.0, 2.0 +ans);//放大到100倍
printf("Case %d: %d %03lld\n",++cas,res,sum);
}
system("pause");
return 0;
}