[LightOJ1282]Leading and Trailing

题目链接: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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int mod = 1000;
10 LL n, k;
11 
12 LL quickmul(LL x, LL n) {
13     LL ans = 1;
14     LL t = x;
15     while(n) {
16         if(n & 1) {
17             ans = (ans * t) % mod;
18         }
19         t = t * t % mod;
20         n >>= 1;
21     }
22     return ans;
23 }
24 
25 LL mul(LL x, LL n) {
26     LL a, b;
27     LL answer;
28     double ans = n * log10(x);
29     b = (LL)ans;
30     a = (LL)(ans * 10000000) - b * 10000000;
31     answer = (LL)(pow(10, 1.0 * a / 10000000) * 100);
32     return answer;
33 }
34 
35 pair<LL, LL> solve() {
36     LL l;
37     LL t;
38     l = mul(n, k);
39     t = quickmul(n, k) % mod;
40     return make_pair(l, t);
41 }
42 int main() {
43     int T;
44     scanf("%d", &T);
45     for(int ii = 1; ii <= T; ii++) {
46         scanf("%I64d %I64d", &n, &k);
47         solve();
48         pair<LL, LL> tmp = solve();
49         printf("Case %d: %d %03d
", ii, (int)tmp.first, (int)tmp.second);
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/kirai/p/4743927.html