【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就行了)就是前三位了。
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; }
相关文章推荐
- lightoj 1282 - Leading and Trailing 【数学】
- lightoj-1282-Leading and Trailing【快速幂】(两个数学函数的使用)
- LightOJ - 1282 Leading and Trailing【数学】
- 数论知识点1——快速幂取模-LightOJ 1282
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
- lightoj1138数学—数学小知识点
- LightOJ1282 - Leading and Trailing(快速幂+数学)
- B - Leading and Trailing LightOJ - 1282(数学加快速幂)
- 【lightoj1045】数学-小知识点
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
- LightOJ 1282 Leading and Trailing (数学)
- 基础数学1005 LightOJ 1282
- lightoj 1282 - Leading and Trailing (数学--log使用)
- Lightoj 1282 (数学题)
- LightOJ 1005 - Rooks(组合数学解法,记忆化解法)
- LightOJ - 1282 B - Leading and Trailing——快速幂运算
- 数学学习小记(二) 组合数求模:Lucas 定理 LightOJ 1067 + Hdu 3037
- LightOJ 1282 Leading and Trailing(n阶的前三位,后三位)
- 数学知识点 长期更新.
- [LightOJ1282]Leading and Trailing