[LightOJ1282]Leading and Trailing
2015-08-20 00:28
597 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26992
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
求x^k的前3项和后3项,数据保证这个数有6位。
卡PE卡了好久,求前3位的思路同上一题fibnacci的相似,推一下过程就能写出来。后三位直接快速幂%1000
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
求x^k的前3项和后3项,数据保证这个数有6位。
卡PE卡了好久,求前3位的思路同上一题fibnacci的相似,推一下过程就能写出来。后三位直接快速幂%1000
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int mod = 1000; LL n, k; LL quickmul(LL x, LL n) { LL ans = 1; LL t = x; while(n) { if(n & 1) { ans = (ans * t) % mod; } t = t * t % mod; n >>= 1; } return ans; } LL mul(LL x, LL n) { LL a, b; LL answer; double ans = n * log10(x); b = (LL)ans; a = (LL)(ans * 10000000) - b * 10000000; answer = (LL)(pow(10, 1.0 * a / 10000000) * 100); return answer; } pair<LL, LL> solve() { LL l; LL t; l = mul(n, k); t = quickmul(n, k) % mod; return make_pair(l, t); } int main() { int T; scanf("%d", &T); for(int ii = 1; ii <= T; ii++) { scanf("%I64d %I64d", &n, &k); solve(); pair<LL, LL> tmp = solve(); printf("Case %d: %d %03d\n", ii, (int)tmp.first, (int)tmp.second); } return 0; }
相关文章推荐
- 2015 Multi-University Training Contest 9 1005
- uva 11177 - Fighting Against a Polygonal Monster(几何)
- HDOJ 1789 Doing Homework again (贪心&&优先队列)
- HDOJ 2674 N!Again (同余定理)
- POJ3250--Bad Hair Day(栈)
- CF#310 div2 D set+pair+贪心
- Chain of Responsibility模式
- LeetCode(70) Climbing Stairs
- poj1575 Easier Done Than Said?(模拟,水题)
- LeetCode(70) Climbing Stairs
- hdu 5319 Painter 2015 Multi-University Training Contest 3
- HDU 1532 Drainage Ditches(基础最大流)
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
- POJ 1218 THE DRUNK JAILER
- HDU 3411 Snail Alice
- THE DRUNK JAILER 1218
- 【LeetCode】217_Contains Duplicate
- nyoj77开灯问题 VS poj1218 THE DRUNK JAILER(开关灯问题模板)
- 2015 HUAS Summer Trainning #6~J
- poj 1218&&hdu 1337 The Drunk Jailer【简单模拟】