Lightoj1282(HDOJ1060) - Leading and Trailing

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).

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

后三位直接取该数的后三位快速幂取得

用科学计数法表示n^k      22222=2.2222* 10^4

设x=log10(n^k)=k*log10(n)

那么10^x=n^k   设x=a(整数)+b(小数)

整数部分10^a是小数点的位置 不影响前三位数字

只需求出10^b的前三位(取前1位直接取整  取前三位*100再取整)

小数部分取整

如果输出结果不够3位要补0

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
long long  hou(long long x,long long y)//快速幂 
{
	 x=x%1000;
	long long res=1; 
	while(y)
	{
		if(y&1)
     	{
		res=res*x%1000;
	   }
	   y=y/2;
	   x=x*x%1000;
	}	
	return res;
}
int main()
{
	int t;
	int ans=0; 
	scanf("%d",&t);
	while(t--)
	{
		ans++;
		 long long  n,k;
		scanf("%lld%lld",&n,&k);
		printf("Case %d: ",ans);
	    double x=(log10(n))*(double)k;//科学计数法 
	          x=x-(long long)x;
		   double qian=pow(10,x);
	     long long aaa=(long long)(qian*100);
		printf("%lld ",aaa);
		long long  hs=hou(n,k);
		if(hs<10)
		{
			printf("00%lld\n",hs);
		}
		else
		{
		  if(hs<100)
		  {
		  	 printf("0%lld\n",hs);
		  }
	     	else
		   {
		    printf("%lld\n",hs); 
		   }
		}
	
	}
	return 0;
}



posted @ 2019-12-12 09:00  千金一发  阅读(64)  评论(0编辑  收藏  举报