您的位置:首页 > 其它

【lightoj1282】数学-小知识点

2016-08-23 21:53 295 查看
F - F 使用long long
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status Practice LightOJ
1282 

uDebug


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=10^(x+y),n的k次幂可以写成10的(x+y)次幂,其中10^x决定n^k的位数10^y决定每一位的数字,那么,log10(n^k)=k*log10(n)减去它的整数部分(LL一下)得到小数部分,再乘100(因为大家都学过10的小数次方一定大于1,所以10^y一定是个位数字小于10大于1的数,然后跟一串小数,已有一位整数,在乘100就行了)就是前三位了。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
LL ex_pow(LL n,LL k,LL x) {
LL ans=1,base=n;
while(k) {
if(k&1) {
ans=ans*base%x;
}
base=base*base%x;
k>>=1;
}
return ans;
}
int main() {
int T,p=0;
scanf("%d",&T);
while(T--) {
LL n,k;
scanf("%d%d",&n,&k);
double ans=1.0*k*log10(n*1.0);
ans=ans-(LL)ans;
LL key=ex_pow(n,k,1000);
printf("Case %d: %03lld %03lld\n",++p,(LL)(pow(10,ans)*100),key);//开始没考虑全面忘了最大达不到三位的情况了,wa了一次
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学