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
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int _ = (1 << 20) + 7;
const int mod = 998244353;
int Pw(int a, int p) {
int res = 1;
while (p) {
if (p & 1) res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
p >>= 1;
}
return res;
}
namespace Poly {
int tot, num[_], pwrt[_], inv[_], a[_], b[_];
unsigned long long q[_];
void Init(int n) {
tot = 1; while (tot < n + n) tot <<= 1;
inv[1] = 1; for (int i = 2; i <= tot; ++i) inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod;
pwrt[0] = 1, pwrt[1] = Pw(3, (mod - 1) / tot);
for (int i = 2; i <= tot; ++i) pwrt[i] = 1ll * pwrt[i - 1] * pwrt[1] % mod;
}
void NTT(int *f, bool ty, int t = tot) {
for (int i = 0; i < t; ++i) {
num[i] = (num[i >> 1] >> 1) | ((i & 1) ? t >> 1 : 0);
q[i] = f[num[i]];
}
for (int len = 2; len <= t; len <<= 1) {
int gap = len >> 1, d = tot / len;
for (int i = 0; i < t; i += len)
for (int j = 0; j < gap; ++j) {
int tmp = q[i + j + gap] * pwrt[ty ? tot - j * d : j * d] % mod;
q[i + j + gap] = q[i + j] - tmp + mod;
q[i + j] = q[i + j] + tmp;
}
}
for (int i = 0; i < t; ++i) f[i] = q[i] * (ty ? inv[t] : 1) % mod;
}
void Mul(int *h, int *f, int *g, int t = tot) {
memcpy(a, f, t << 2), memcpy(b, g, t << 2), memset(h, 0, t << 2);
NTT(a, 0, t), NTT(b, 0, t);
for (int i = 0; i < t; ++i) h[i] = 1ll * a[i] * b[i] % mod;
NTT(h, 1, t);
}
void Inv(int *h, int *f, int Len) {
memset(a, 0, Len << 3), memset(b, 0, Len << 3);
memcpy(b, f, sizeof b); memset(h, 0, Len << 3);
h[0] = Pw(b[0], mod - 2), a[0] = b[0], a[1] = b[1];
for (int len = 2, t = 4; len <= Len; len <<= 1, t <<= 1) {
NTT(a, 0, t), NTT(h, 0, t);
for (int i = 0; i < t; ++i) h[i] = 1ll * h[i] * (2 - 1ll * h[i] * a[i] % mod + mod) % mod;
NTT(h, 1, t);
memcpy(a, b, t << 2), memset(h + len, 0, len << 2);
}
}
}
int n, m, F[_], G[_], H[_], A[_], B[_], C[_], T0[_], T1[_], T2[_], inv24 = 291154603;
int main() {
cin >> n; m = (n & 1) ? n / 2 + 1 : n / 2; Poly::Init(2 * (n + 1));
H[0] = A[0] = B[0] = 1;
int len, t;
for (len = 2, t = 4; len <= 2 * (m + 1); len <<= 1, t <<= 1) {
memset(T0 + len, 0, len << 2);
for (int i = 0; i < len; ++i) T0[i] = 2ll * A[i] % mod;
Poly::Mul(T1, B, H, t);
for (int i = 0; i < len; ++i) T0[i] = (T0[i] + 3ll * T1[i] % mod) % mod;
Poly::Mul(T2, H, H, t), Poly::Mul(T1, T2, H, t);
for (int i = len - 1; i; --i) T0[i] = (T0[i - 1] + T1[i - 1]) % mod;
T0[0] = 6;
for (int i = 0; i < len / 2; ++i) T0[i] = (T0[i] - 6ll * H[i] % mod + mod) % mod;
for (int i = len - 1; i; --i) T1[i] = 3ll * (B[i - 1] + T2[i - 1]) % mod;
T1[0] = mod - 6;
Poly::Inv(T1, T1, len);
memset(T1 + len, 0, len << 2);
Poly::Mul(T0, T0, T1, t);
for (int i = 0; i < len; ++i) H[i] = (H[i] - T0[i] + mod) % mod, A[i] = B[i] = 0;
for (int i = 0; i < len * 2 / 3; ++i) A[i * 3] = H[i];
for (int i = 0; i < len; ++i) B[i * 2] = H[i];
}
if (t > Poly::tot) t >>= 1, len >>= 1;
memset(T0, 0, sizeof T0), memset(A, 0, sizeof A), memset(B, 0, sizeof B), memset(C, 0, sizeof C);
memcpy(T0, H, m << 2);
for (int i = 0; i < len / 2; ++i) A[i * 4] = T0[i];
for (int i = 0; i < len * 2 / 3; ++i) B[i * 3] = T0[i];
for (int i = 0; i < len; ++i) C[i * 2] = T0[i];
memset(A + len, 0, len << 2), memset(B + len, 0, len << 2), memset(C + len, 0, len << 2);
for (int i = 0; i < len; ++i) F[i] = 6ll * A[i] % mod;
Poly::Mul(T1, B, T0, t);
for (int i = 0; i < len; ++i) F[i] = (F[i] + 8ll * T1[i] % mod) % mod;
Poly::Mul(T1, C, C, t);
for (int i = 0; i < len; ++i) F[i] = (F[i] + 3ll * T1[i] % mod) % mod;
Poly::Mul(T1, T0, T0, t), memset(T1 + len, 0, len << 2), Poly::Mul(T1, T1, C);
for (int i = 0; i < len; ++i) F[i] = (F[i] + 6ll * T1[i] % mod) % mod;
Poly::Mul(T1, T0, T0, t), memset(T1 + len, 0, len << 2), Poly::Mul(T1, T1, T1, t);
for (int i = 0; i < len; ++i) F[i] = (F[i] + T1[i]) % mod;
for (int i = len - 1; i; --i) F[i] = 1ll * inv24 * F[i - 1] % mod;
F[0] = 1;
if (n & 1) printf("%d\n", F[n]);
else {
int ans = 1ll * (H[n / 2] + 1ll * H[n / 2] * H[n / 2] % mod) * Pw(2, mod - 2) % mod;
printf("%d\n", (ans + F[n]) % mod);
}
return 0;
}
|