您的位置:首页 > 大数据 > 人工智能

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就是前三位数.

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: