hdu7057 Buying Snacks

齐次递推题(可矩阵快速幂)

HDU7057Buying Snacks

题目大意

给 n 种零食,每个零食有大小包装之分,有三种购买方案,价钱分别如下:

只购买一个小的,花费一块钱
只独购买一个大的,花费两块钱
$ \forall i>1 ,可以和 第i-1$种零食捆绑购买,可以便宜一块钱
她有m元钱,问她的不同购买方案数分别是多少

题目分析

可以考虑出来状态转移方程,dp[i][j]=dp[i1][j]+dp[i1][j1]+dp[i1][j2]+dp[i2][j1]+2dp[i2][j2]+dp[i2][j3]dp[i][j]=dp[i-1][j]+dp[i-1][j-1]+dp[i-1][j-2]+dp[i-2][j-1]+2dp[i-2][j-2]+dp[i-2][j-3]

转化为生成函数,fi=(1+x+x2)fi1+(x+2x+x3)fi2f_i=(1+x+x^2)f_{i-1}+(x+2x+x^3)f_i-2,然后列出特征方程,Z2=(1+X+X2)Z+(X+2X2+X3)Z^2=(1+X+X^2)Z+(X+2X^2+X^3)求解出来得到z1=x2+2x+1,z2=xz_1=x^2+2x+1,z_2=-x通过已知条件f0=1,f1=1+x+x2f_0=1,f_1=1+x+x^2进行待定系数法解方程,得到通项公式:fn=(x2+2x+1)n+1+(1)nxn+1x2+3x+1f_n=\frac{(x^2+2x+1)^{n+1}+(-1)^nx^{n+1}}{x^2+3x+1}

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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// Created by mrx on 2022/8/6.
//
#include <bits/stdc++.h>

using ll = long long;

template<typename T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<typename T>
T power(T a, int b) {
T ans = 1;
for (; b; a *= a, b >>= 1) {
if (b & 1)ans *= a;
}
return ans;
}

template<int Mod>
class Modular {
public:
using Type = int;

template<typename U>
static Type norm(U &x) {
Type v;
if (-Mod <= x && x < Mod) v = static_cast<Type>(x);
else v = static_cast<Type>(x % Mod);
if (v < 0) v += Mod;
return v;
}

constexpr Modular() : value() {}

int val() const { return value; }

Modular inv() const {
return Modular(inverse(value, Mod));
}

template<typename U>
Modular(const U &x) {
value = norm(x);
}

const Type &operator()() const {
return value;
}

template<typename U>
explicit operator U() const {
return static_cast<U>(value);
}

Modular &operator+=(const Modular &other) {
if ((value += other.value) >= Mod) value -= Mod;
return *this;
}

Modular &operator-=(
const Modular &other) {
if ((value -= other.value) < 0) value += Mod;
return *this;
}

template<typename U>
Modular &operator+=(const U &other) { return *this += Modular(other); }

template<typename U>
Modular &operator-=(const U &other) { return *this -= Modular(other); }

Modular &operator++() { return *this += 1; }

Modular &operator--() { return *this -= 1; }

Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}

Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}

Modular operator-() const { return Modular(-value); }

template<class ISTREAM_TYPE>
friend ISTREAM_TYPE &operator>>(ISTREAM_TYPE &is, Modular &rhs) {
ll v;
is >> v;
rhs = Modular(v);
return is;
}

template<class OSTREAM_TYPE>
friend OSTREAM_TYPE &operator<<(OSTREAM_TYPE &os, const Modular &rhs) {
return os << rhs.val();
}

Modular &operator*=(const Modular &rhs) {
value = ll(value) * rhs.value % Mod;
return *this;
}

Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, Mod)); }

friend const Type &abs(const Modular &x) { return x.value; }

friend bool operator==(const Modular &lhs, const Modular &rhs) { return lhs.x == rhs.x; }

friend bool operator<(const Modular &lhs, const Modular &rhs) { return lhs.x < rhs.x; }


bool operator==(const Modular &rhs) { return *this == rhs.value; }

template<typename U>
bool operator==(U rhs) { return *this == Modular(rhs); }

template<typename U>
friend bool operator==(U lhs, const Modular &rhs) { return Modular(lhs) == rhs; }

bool operator!=(const Modular &rhs) { return *this != rhs; }

template<typename U>
bool operator!=(U rhs) { return *this != rhs; }

template<typename U>
friend bool operator!=(U lhs, const Modular &rhs) { return lhs != rhs; }

bool operator<(const Modular &rhs) { return this->value < rhs.value; }

Modular operator+(const Modular &rhs) { return Modular(*this) += rhs; }

template<typename U>
Modular operator+(U rhs) { return Modular(*this) += rhs; }

template<typename U>
friend Modular operator+(U lhs, const Modular &rhs) { return Modular(lhs) += rhs; }

Modular operator-(const Modular &rhs) { return Modular(*this) -= rhs; }

template<typename U>
Modular operator-(U rhs) { return Modular(*this) -= rhs; }

template<typename U>
friend Modular operator-(U lhs, const Modular &rhs) { return Modular(lhs) -= rhs; }

Modular operator*(const Modular &rhs) { return Modular(*this) *= rhs; }

template<typename U>
Modular operator*(U rhs) { return Modular(*this) *= rhs; }

template<typename U>
friend Modular operator*(U lhs, const Modular &rhs) { return Modular(lhs) *= rhs; }

Modular operator/(const Modular &rhs) { return Modular(*this) /= rhs; }

template<typename U>
Modular operator/(U rhs) { return Modular(*this) /= rhs; }

template<typename U>
friend Modular operator/(U lhs, const Modular &rhs) { return Modular(lhs) /= rhs; }

private:
Type value;
};

constexpr int mod = 998244353;
using Z = Modular<mod>;

std::vector<int> rev;
std::vector<Z> roots{0, 1};

void dft(std::vector<Z> &a) {
int n = a.size();

if (int(rev.size()) != n) {
rev.resize(n);
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);
}
}

for (int i = 0; i < n; ++i) {
if (rev[i] < i)std::swap(a[i], a[rev[i]]);
}
if (int(roots.size() < n)) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = power(Z(3), (mod - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[i << 1] = roots[i];
roots[i << 1 | 1] = roots[i] * e;
}
k++;
}
}

for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}

void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
}

struct Poly {
std::vector<Z> a;

Poly() {}

Poly(const std::vector<Z> &a) : a(a) {}

Poly(const std::initializer_list<Z> &a) : a(a) {}

int size() const {
return a.size();
}

void resize(int n) {
a.resize(n);
}

Z operator[](int idx) const {
if (idx < size()) {
return a[idx];
} else {
return 0;
}
}

Z &operator[](int idx) {
return a[idx];
}

Poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}

Poly modxk(int k) const {
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}

Poly divxk(int k) const {
if (size() <= k) {
return Poly();
}
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}

friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return Poly(res);
}

friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return Poly(res);
}

friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
Z inv = Z(sz).inv();
for (int i = 0; i < sz; ++i) {
a.a[i] = a[i] * b[i] * inv;
}
idft(a.a);
a.resize(tot);
return a;
}

friend Poly operator*(Z a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}

friend Poly operator*(Poly a, Z b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}

Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}

Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}

Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}

Poly deriv() const {
if (a.empty()) {
return Poly();
}
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}

Poly integr() const {
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}

Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}

Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}

Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}

Poly pow(int k, int m) const {
int i = 0;
while (i < size() && a[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(std::vector<Z>(m));
}
Z v = a[i];
auto f = divxk(i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
}

Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((mod + 1) / 2);
}
return x.modxk(m);
}

Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}

std::vector<Z> eval(std::vector<Z> x) const {
if (size() == 0) {
return std::vector<Z>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};

void sol() {
int n, k;
std::cin >> n >> k;
Poly inv = Poly{1, 3, 1}.inv(k + 1);
Poly z1 = Poly{1, 2, 1}.pow(n + 1, k + 1);
if (k >= n + 1) {
if (n & 1) {
z1[n + 1]--;
} else {
z1[n + 1]++;
}
}
z1 *= inv;
for (int i = 1; i <= k; ++i)std::cout << z1[i] << ' ';
std::cout << '\n';
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
#endif
int testcase;
std::cin >> testcase;
while (testcase--)sol();
return 0;
}

hdu7057 Buying Snacks
https://mrxyan6.github.io/2022/09/07/hdu7057/
作者
mrx
发布于
2022年9月7日
许可协议