-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0011.cpp
More file actions
132 lines (111 loc) · 2.21 KB
/
0011.cpp
File metadata and controls
132 lines (111 loc) · 2.21 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// https://acmp.ru/index.asp?main=task&id_task=11
// arbitrary precision arithmetic
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
static const int BASE = 1000000000;
struct lnd { int val; lnd* pr; lnd* nx; };
struct bn { lnd* h; lnd* t; };
static bn* bn_alloc()
{
bn* res = new bn;
res->h = NULL;
res->t = NULL;
return res;
}
static void bn_push(bn* b, int v)
{
lnd* n = new lnd;
n->val = v;
n->nx = NULL;
n->pr = b->t;
if (b->t) b->t->nx = n;
else b->h = n;
b->t = n;
}
static bn* bn_init(int v)
{
bn* res = bn_alloc();
if (v == 0)
{
bn_push(res, 0);
return res;
}
while (v > 0)
{
bn_push(res, v % BASE);
v /= BASE;
}
return res;
}
static void bn_free(bn* b)
{
if (!b) return;
lnd* cur = b->h;
while (cur)
{
lnd* tmp = cur;
cur = cur->nx;
delete tmp;
}
delete b;
}
static bn* bn_add(bn* a, bn* b)
{
bn* res = bn_alloc();
lnd* pa = a ? a->h : NULL;
lnd* pb = b ? b->h : NULL;
ll cr = 0;
while (pa || pb || cr)
{
ll sum = cr;
if (pa) { sum += pa->val; pa = pa->nx; }
if (pb) { sum += pb->val; pb = pb->nx; }
cr = sum / BASE;
bn_push(res, (int)(sum % BASE));
}
if (!res->h) bn_push(res, 0);
return res;
}
static void bn_print(bn* b)
{
if (!b || !b->t) { cout << "0\n"; return; }
cout << b->t->val;
lnd* cur = b->t->pr;
while (cur)
{
cout << setw(9) << setfill('0') << cur->val;
cur = cur->pr;
}
cout << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int k, n;
if (!(cin >> k >> n)) return 0;
if (n == 0)
{
cout << "0\n";
return 0;
}
vector<bn*> dp(n + 1, NULL);
dp[0] = bn_init(1);
rep(i, 1, n + 1)
{
dp[i] = bn_init(0);
rep(j, 1, k + 1)
if (i - j >= 0)
{
bn* tmp = bn_add(dp[i], dp[i - j]);
bn_free(dp[i]);
dp[i] = tmp;
}
}
bn_print(dp[n]);
rep(i, 0, n + 1) bn_free(dp[i]);
return 0;
}