[AHOI2002]网络传输

这道题根据题意,易知k的幂与p的二进制形式有关系,然后再一波高精度即可。(这里我用$n、k$代替了$k、p$)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 #define re register
 9 #define rep(i, a, b) for (re int i = a; i <= b; ++i)
10 #define repd(i, a, b) for (re int i = a; i >= b; --i)
11 #define maxx(a, b) a = max(a, b);
12 #define minn(a, b) a = min(a, b);
13 #define LL long long
14 #define inf (1 << 30)
15 
16 inline int read() {
17     int w = 0, f = 1; char c = getchar();
18     while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar();
19     while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar();
20     return w * f;
21 }
22 
23 const int maxl = 15, base = 10000;
24 
25 int n, k;
26 
27 int a[maxl], sum[maxl];
28 
29 void time() {
30     int x = 0;
31     rep(i, 1, a[0]) a[i] = a[i] * n + x, x = a[i] / base, a[i] %= base;
32     if (x) a[++a[0]] = x;
33 }
34 
35 void add() {
36     int x = 0; maxx(sum[0], a[0]);
37     rep(i, 1, sum[0]) sum[i] += a[i] + x, x = sum[i] / base, sum[i] %= base;
38     if (x) sum[++sum[0]] = x;
39 }
40 
41 int main() {
42     freopen("ques.in", "r", stdin);
43     freopen("ques.out", "w", stdout);
44 
45     n = read(), k = read();
46     a[0] = a[1] = sum[0] = 1;
47 
48     while (k) {
49         if (k & 1) add();
50         k >>= 1;
51         time();
52     }
53 
54     repd(i, sum[0], 1)
55         if (i == sum[0]) printf("%d", sum[i]); else printf("%04d", sum[i]);
56 
57     return 0;
58 }

 

posted @ 2019-01-23 17:26  AC-Evil  阅读(138)  评论(1编辑  收藏  举报