ayit第十五周训练题题i题

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

#include
#include
#include
//#include
//using namespace std;
int n,k;
int e(int n,int k)
{
    int a1=1,b1=n%1000;
    while(k)
    {
        if(k%2==1)
        {
            a1=(a1*b1)%1000;
        }
        b1=(b1*b1)%1000;
        k/=2;
    }
    return a1;
}
int main()
{
    int t,tt=1,cc;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        double a,a2;
        a=(double)k*log10(n*1.0);
        a-=(int)a;
        a2=pow(10.0,a);
        cc=e(n,k);
        printf("Case %d: %d %03d\n",tt++,(int)(a2*100),cc);
    }
    return 0;
}

题意   求一个数n的k次方后的前三位与后三位。并且后三位要求控制格式

思路   后三位可以用快速幂求出来,前三位要用到log了

double p=k*log10(n);
p=p-(int)p;
int ans=(int)(pow(10,p)*100);
前三位求法。

你可能感兴趣的:(ayit第十五周训练题题i题)