Description
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).
OutputFor 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 Input5
123456 1
123456 2
2 31
2 32
29 8751919
Sample OutputCase 1: 123 456
Case 2: 152 936
Case 3: 214 648
Case 4: 429 296
Case 5: 665 669
题意:求n的k次方的前三位和后三位,后三位要补前导零。
思路:
这题的难点在于前三个数字的推法。后三个数用快速幂即可。这题得先把n^k变为10^a形式,也就是n^k=10^a;求出a。我在纸上推了一下。
![](https://i-blog.csdnimg.cn/blog_migrate/1838cef14c2a2df4edb1c8b277412b62.png)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define mod 1000
using namespace std;
long long kum(long long n,long long k)
{
long long ans=1;
while(k)
{
if(k&1)
{
ans*=n;
ans%=mod;
}
k/=2;
n*=n;
n%=mod;
}
return ans;
}
int main()
{
long long n,k,ans2,ans1;
int hp=1,t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
ans1=pow(10,fmod(k*log10(n),1)+2);
ans2=kum(n,k);
printf("Case %d: %lld %03lld\n",hp++,ans1,ans2);
}
}