-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfast_hadmard_transform.cpp
More file actions
178 lines (159 loc) · 4.78 KB
/
Copy pathfast_hadmard_transform.cpp
File metadata and controls
178 lines (159 loc) · 4.78 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
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
//
// 添字 XOR 畳み込み (高速アダマール変換)
//
// verified:
// ABC 212 H - Nim Counting
// https://atcoder.jp/contests/abc212/tasks/abc212_h
//
#include <bits/stdc++.h>
using namespace std;
// Fast Hadamard Transform
// N must be 2^K for some K
namespace FastHadamardTransform {
template<class T> void trans(vector<T> &v, bool inv = false) {
int N = v.size();
for (int i = 1; i < N; i <<= 1) {
for (int j = 0; j < N; ++j) {
if ((j & i) == 0) {
auto x = v[j], y = v[j | i];
v[j] = x + y;
v[j | i] = x - y;
if (inv) v[j] /= 2, v[j | i] /= 2;
}
}
}
}
template<class T> vector<T> mul(const vector<T> &a, const vector<T> &b) {
int N = a.size();
auto A = a, B = b;
trans(A), trans(B);
vector<T> C(N);
for (int i = 0; i < N; ++i) C[i] = A[i] * B[i];
trans(C, true);
return C;
}
};
using namespace FastHadamardTransform;
// modint
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() const { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b, swap(a, b);
u -= t * v, swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept {
is >> x.val;
x.val %= MOD;
if (x.val < 0) x.val += MOD;
return is;
}
friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD>& r, long long n) noexcept {
if (n == 0) return 1;
if (n < 0) return modpow(modinv(r), -n);
auto t = modpow(r, n / 2);
t = t * t;
if (n & 1) t = t * r;
return t;
}
friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b, swap(a, b);
u -= t * v, swap(u, v);
}
return Fp<MOD>(u);
}
};
//------------------------------//
// Examples
//------------------------------//
const int MOD = 998244353;
using mint = Fp<MOD>;
// 級数和を求める
vector<mint> operator + (const vector<mint> &f, const vector<mint> &g) {
vector<mint> res(f.size());
for (int i = 0; i < f.size(); ++i) {
res[i] = f[i] + g[i];
}
return res;
}
vector<mint> operator * (const vector<mint> &f, const vector<mint> &g) {
vector<mint> res(f.size());
for (int i = 0; i < f.size(); ++i) {
res[i] = f[i] * g[i];
}
return res;
}
// 級数和
// unit: 冪乗演算の単位元
template<class T> T calc_series(T v, long long N, T unit) {
if (N == 1) return unit;
if (N % 2 == 1)
return v * calc_series(v, N - 1, unit) + unit;
else
return (v + unit) * calc_series(v * v, N / 2, unit);
}
// ベクトルサイズ
const int V = 65536;
int main() {
// 入力
int N, K;
cin >> N >> K;
vector<mint> f(V);
for (int i = 0; i < K; ++i) {
int a;
cin >> a;
f[a] = 1;
}
// 全体の答え
mint all = mint(K) * calc_series(mint(K), N, mint(1));
// 級数和を求める
vector<mint> unit(V, 0);
unit[0] = 1;
trans(f), trans(unit); // 高速アダマール変換
auto res = f * calc_series(f, N, unit);
trans(res, true);
cout << all - res[0] << endl;
}