题解 - [LightOJ 1282] Leading and Trailing

题目链接

简述题意

给定 \(n\)\(k\), 输出 \(n^k\) 的前三位和后三位

解题思路

后三位直接用快速幂即可

前三位考虑取对数,截取小数部分,我们便可以通过这个还原出前三位了

代码

Show code

LightOJ_1282view raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <cmath>
#include <cstdio>
const int MOD = 1000;
template <typename T>
T qpow(T a, T b, T mod = MOD) {
T res = 1;
for (; b; b >>= 1, (a *= a) %= mod)
if (b & 1) (res *= a) %= mod;
return res;
}
int main() {
double _;
int kase;
scanf("%d", &kase);
for (int cnt = 1; cnt <= kase; ++cnt) {
i64 a, k;
scanf("%lld%lld", &a, &k);
printf("Case %d: %d %03d\n",
cnt,
(int)floor(pow(10, modf(log10(a) * k, &_)) * 100),
qpow(a, k));
}
return 0;
}