-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10523.cpp
49 lines (41 loc) · 1.05 KB
/
10523.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdio>
#include <cstring>
#define BASE 1000000000
using namespace std;
struct bignum {
int l;
long d[30];
} sum, base;
int main(void)
{
int n, a, i, j;
while (scanf("%d %d", &n, &a) != EOF) {
memset(sum.d, 0, sizeof(sum.d));
sum.l = 1;
memset(base.d, 0, sizeof(base.d));
base.l = 1;
base.d[0] = a;
for (i = 1; i <= n; ++i) {
for (j = 0; j < base.l; ++j) {
sum.d[j] += base.d[j] * i;
base.d[j] *= a;
}
for (j = 0; j < base.l; ++j) {
base.d[j + 1] += base.d[j] / BASE;
base.d[j] %= BASE;
}
if (base.d[j] > 0) ++base.l;
for (j = 0; j < sum.l; ++j) {
sum.d[j + 1] += sum.d[j] / BASE;
sum.d[j] %= BASE;
}
if (sum.d[j] > 0) ++sum.l;
}
i = sum.l - 1;
printf("%ld", sum.d[i]);
while (--i > -1)
printf("%09ld", sum.d[i]);
printf("\n");
}
return 0;
}