【lightoj1282】数学-小知识点

深藏阁楼爱情的钟 2022-07-17 00:28 252阅读 0赞

F - F 使用long long

Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1282 12x12_uDebug.pnguDebug

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就行了)就是前三位了。

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define LL long long
  6. using namespace std;
  7. LL ex_pow(LL n,LL k,LL x) {
  8. LL ans=1,base=n;
  9. while(k) {
  10. if(k&1) {
  11. ans=ans*base%x;
  12. }
  13. base=base*base%x;
  14. k>>=1;
  15. }
  16. return ans;
  17. }
  18. int main() {
  19. int T,p=0;
  20. scanf("%d",&T);
  21. while(T--) {
  22. LL n,k;
  23. scanf("%d%d",&n,&k);
  24. double ans=1.0*k*log10(n*1.0);
  25. ans=ans-(LL)ans;
  26. LL key=ex_pow(n,k,1000);
  27. printf("Case %d: %03lld %03lld\n",++p,(LL)(pow(10,ans)*100),key);//开始没考虑全面忘了最大达不到三位的情况了,wa了一次
  28. }
  29. return 0;
  30. }

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读