码迷,mamicode.com
首页 > 其他好文 > 详细

[LightOJ1282]Leading and Trailing

时间:2015-08-20 01:15:52      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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\n", ii, (int)tmp.first, (int)tmp.second);
50     }
51     return 0;
52 }

 

[LightOJ1282]Leading and Trailing

标签:

原文地址:http://www.cnblogs.com/vincentX/p/4743927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!