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 nkcontains 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的k次方的前三位和后四位。
思路:后三位(快速幂取模1000),前三位(求出log10(n^k)=k*log10(n)取余1(用fmod)求出剩下的浮点数再让浮点数乘以10的(2+fmod(k*log10(n),1)次方。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int pow_mod(int n,int k,int mod)
{
int res=1;
while(k)
{
if(k&1)
res=res*n%mod;
n=n*n%mod;
k>>=1;
}
return res%mod;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
int n,k;
scanf("%d %d",&n,&k);
int low=pow_mod(n%1000,k,1000);
int high=pow(10,2+fmod(k*log10(n),1));
printf("Case %d: %03d %03d\n",cas,high,low);
}
return 0;
}