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的前三位和后三位

思路:后三位快速幂就行了,关键是这个前三位。

设x = log10 (n^k)  = k * log10(n),那么10^x = n^k.将x = a(整数) + b(小数),a决定了10^k的位数,b决定了10^k的具体数字。

double p=k*log10(n);
p=fmod(p,1);  (fmod是浮点数取模)
int ans=(int)(pow(10,p)*100);

这样就求出了答案。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define maxn 1000000+1000
#define ll long long

int t,n,k;

int pow_mod(int a,int n,int k)
{
	if(!n)return 1;
	ll x=pow_mod(a,n/2,k);
	ll ans=x*x%k;
	if(n&1)ans=ans*a%k;
	return (int)ans;
}

int main()
{
	//freopen("input.in","r",stdin);
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		cin>>n>>k;
		printf("Case %d: %03d %03d\n",i,(int)(pow(10,fmod(k*log10(n),1))*100),pow_mod(n,k,1000));
	}
	return 0;
}