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

这个题真不会,,,看了好多题解。。。我尽量说的明白些。。

   题意:给你一个数n,让你求这个数的k次方的前三位和最后三位 

   思路:最后的三位,可以通过直接取余得到;(可以参考《算法入门经典》刘汝佳  p181例10-2 幂取模)

至于前三位,才是难点。这个数那么大,肯定不能直接算。。。数据会崩溃。但我们还必须算出来,那你想怎么办了吗。把数变小,那怎么变小呢。

因为是n^k幂,自然而然的想到对数,log.那取几呢,肯定怎么好算怎么取啦。一般都会想10好算。。但这只是原因之一。另一方面。一个数都可以用10^a表示。那这里怎么用的呢。10^0=1,.这里a是double型。因为你不知道n^k,是不是10的倍数,a可能是一个整数+小数.。

说了半天,还有一个重要的一步。k*lg(n*1.0)这样数变小了吧。接下来我们找他的小数,整数是提供位数呢,不信,自己想想看。小数才是我们要的。n^k=(10^a)^k=10^a*k=(10^x)*(10^y);其中x,y分别是a*k的整数部分和小数部分对于t=n^k这个数,它的位数由(10^x)决定,它的位数上的值则有(10^y)决定。 接下来,要用到一个函数fmod,(我也是刚注意到)含在math.h中。fmod(x,y)就是求x/y的余数,这里y=1就是小数啦。

还有最后输出后三位时不清楚为什么要用%03d,           %3d  %d 都不行。。我也是醉了,可能lightoj有·点不一样,

#include<stdio.h>
#include<math.h>
#define mod 1000;
int pow_mod(int a,int n)
{
    if(n==1)return a%mod;
    long long int t=pow_mod(a,n/2);
    long long int ans=t*t%mod;
    if(n%2==1)ans=ans*a%mod;
    return ans;
}
int main()
{
    int T,k,n,f=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        double x=pow(10.0,fmod(k*log10(1.0*n),1));//前三位
        int y=pow_mod(n,k);//后三位
        printf("Case %d: %d %03d\n",f++,(int)(x*100),y);//别忘*100
    }
    return 0;
}


posted on 2017-08-16 15:03  zitian246  阅读(105)  评论(0编辑  收藏  举报