-
Notifications
You must be signed in to change notification settings - Fork 0
/
1997F.cpp
273 lines (239 loc) · 5.33 KB
/
1997F.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Created at: Wed Jul 31 15:33:30 CST 2024
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
const int MOD = 998244353;
/* Snippet BEGIN */
template <class T> constexpr T power(T a, i64 b)
{
T res{1};
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p)
{
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template <i64 P> struct MInt
{
i64 x;
constexpr MInt() : x{0} {}
constexpr MInt(i64 x) : x{norm(x % P)} {}
constexpr i64 norm(i64 x) const
{
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
constexpr i64 val() const { return x; }
constexpr MInt operator-() const
{
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const { return power(*this, P - 2); }
constexpr MInt &operator*=(MInt rhs) &
{
if constexpr (P < (1ULL << 31)) {
x = x * rhs.x % int(P);
} else {
x = mul(x, rhs.x, P);
}
return *this;
}
constexpr MInt &operator+=(MInt rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs)
{
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs)
{
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs)
{
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs)
{
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr bool operator==(MInt lhs, MInt rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs)
{
return lhs.val() != rhs.val();
}
friend constexpr bool operator<(MInt lhs, MInt rhs)
{
return lhs.val() < rhs.val();
}
};
constexpr int P = MOD;
using Z = MInt<P>;
struct Comb
{
int n;
std::vector<Z> _fac;
std::vector<Z> _invfac;
std::vector<Z> _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(int n) : Comb() { init(n); }
void init(int m)
{
m = std::min<i64>(m, P - 1);
if (m <= n)
return;
_fac.resize(m + 1);
_invfac.resize(m + 1);
_inv.resize(m + 1);
for (int i = n + 1; i <= m; i++) {
_fac[i] = _fac[i - 1] * i;
}
_invfac[m] = _fac[m].inv();
for (int i = m; i > n; i--) {
_invfac[i - 1] = _invfac[i] * i;
_inv[i] = _invfac[i] * _fac[i - 1];
}
n = m;
}
Z fac(int m)
{
if (m > n)
init(2 * m);
return _fac[m];
}
Z invfac(int m)
{
if (m > n)
init(2 * m);
return _invfac[m];
}
Z inv(int m)
{
if (m > n)
init(2 * m);
return _inv[m];
}
Z binom(int n, int m)
{
if (n < m || m < 0)
return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
void modint_example()
{
int n = 6;
Comb comb(n);
assert(comb.binom(3, 2) == 3);
Z x = 5;
assert(1 == x / 5);
assert(1 == x * comb.inv(5));
printf("%lld\n", (x / 2).val());
}
dbg_export(Z, val());
/* Snippet END */
int n, x, m;
Z solve()
{
vector<int> fib = {0, 1, 1};
for (int i = 3; i <= x; i++) {
fib.push_back(fib[i - 1] + fib[i - 2]);
}
int upper = fib[x] * n;
dbg(fib[x], upper);
while (fib.back() <= upper) {
int last = fib.back();
int last2 = fib[fib.size() - 2];
int v = last + last2;
fib.push_back(v);
}
fib.pop_back();
dbg(fib);
vector<vector<Z>> dp((size_t)n + 1, vector<Z>(upper + 1, 0));
// dp[num][sum] -> dp[num+1][sum+value];
// -> dp[num+2][sum+value*2];
// -> ...
dp[0][0] = 1;
for (int i = 1; i <= x; i++) {
int value = fib[i];
for (int num = 0; num < n; num++)
for (int sum = 0; sum <= upper; sum++) {
auto &f = dp[num][sum];
// dbg(num, sum, f);
if (f.val() != 0) {
dp[num + 1][sum + value] += f;
}
}
dbg(i, dp);
}
Z ret = 0;
for (int sum = 1; sum <= upper; sum++) {
int count = 0;
int num = sum;
for (int i = fib.size() - 1; i > 0; i--) {
if (fib[i] <= num) {
num -= fib[i];
count += 1;
}
}
assert(num == 0);
dbg(sum, count);
if (count == m) {
ret += dp[n][sum];
}
}
return ret;
}
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> n >> x >> m;
Z ans = solve();
cout << ans.val() << endl;
return 0;
}