您的位置:首页 > 其它

light_oj 1282

2017-08-15 10:42 204 查看
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

题意:有T组数据,每组有n和k,求n的k次方的前三位数和后三位数。
解题思路:后三位好求,算法书上都有介绍,前三位主要运用数学知识,需要把公式推出来,

        前三位化为科学计数法取对数推导:

             n^k=a.bc*10^m ( m为n^k的位数,即m=(int)lg(n^k)=(int)(k*lgn) )

        求对数:  k*lgn=lg(a.bc)+m

        即 a.bc=10^(k*lgn-m)=10^(k*lgn-(int)(k*lgn));

         abc=a.bc*100;

#include <stdio.h>
#include <string.h>
#include <math.h>
typedef long long ll;

ll n;
ll F(ll a,ll b)
{
ll res;
if(b==0)
return 1;
res=F(a,b/2)%1000;
res=res*res%1000;
if(b%2)
res=res*a%1000;
return res;
}
int main()
{
int t,Case=1,k;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
double x=k*log10(n)-(int)(k*log10(n));
ll term=pow(10,x)*100;
printf("Case %d: %lld %03lld\n",Case++,term,F(n%1000,k));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: