LightOJ - 1282 Leading and Trailing【数学】
2018-02-03 21:45
309 查看
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的前三位和最后三位(最后三位不足位置补0).
分析:
后三位直接快速幂取余就行,前三位处理麻烦一点。
nk=10p(p是实数)nk=10p(p是实数).设 p=x(整数)+y(小数)p=x(整数)+y(小数)
=>nk=10x∗10y=>nk=10x∗10y,仔细考虑,小数部分才是决定前三位的关键,10y∗10010y∗100就是前三位数.
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的前三位和最后三位(最后三位不足位置补0).
分析:
后三位直接快速幂取余就行,前三位处理麻烦一点。
nk=10p(p是实数)nk=10p(p是实数).设 p=x(整数)+y(小数)p=x(整数)+y(小数)
=>nk=10x∗10y=>nk=10x∗10y,仔细考虑,小数部分才是决定前三位的关键,10y∗10010y∗100就是前三位数.
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long LL; inline LL quick_pow(LL a, LL b, LL mod) { LL ans = 1; while(b) { if(b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans % mod; } int main() { int T; scanf("%d", &T); while(T--) { LL n, k; scanf("%lld %lld", &n, &k); double ans1 = (double)k * log10((double)n) - (long long)((double)k * log10((double)n)); //注意精度 LL ans3 = (long long)(pow(10, ans1) * 100.0); LL ans2 = quick_pow(n, k, 1000); static int p = 1; printf("Case %d: %lld %03lld\n", p++, ans3, ans2); } return 0; }
相关文章推荐
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
- lightoj 1282 - Leading and Trailing (数学--log使用)
- B - Leading and Trailing LightOJ - 1282(数学加快速幂)
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
- lightoj-1282-Leading and Trailing【快速幂】(两个数学函数的使用)
- LightOJ1282 - Leading and Trailing(快速幂+数学)
- lightoj 1282 - Leading and Trailing 【数学】
- lightoj1282:Leading and Trailing(数论)
- Light-oj-1282 Leading and Trailing(数学取位)
- lightoj 1282 Leading and Trailing
- LightOj 1282 Leading and Trailing
- Lightoj1282——Leading and Trailing(幂取模求前三位)
- Light OJ:1282 Leading and Trailing(数学+思维+对数+快速幂)
- lightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing(取n^k的最高三位数字)
- LightOJ - 1282 Leading and Trailing
- [LightOJ1282]Leading and Trailing
- 【LightOJ1282】Leading and Trailing(数论)
- lightoj1282 - Leading and Trailing
- LightOJ-1282 Leading and Trailing 模算数 快速幂 对数的用法