Skip to content

Commit 821eb83

Browse files
lrvideckisweb-flow
andauthored
Xor conv (#191)
* first draft of xor conv * [auto-verifier] verify commit 7e06c5f * add docs now --------- Co-authored-by: GitHub <noreply@github.com>
1 parent ec7f5bb commit 821eb83

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"tests/library_checker_aizu_tests/convolution/gcd_convolution.test.cpp": "2025-08-28 13:19:16 -0600",
33
"tests/library_checker_aizu_tests/convolution/lcm_convolution.test.cpp": "2025-08-28 13:19:16 -0600",
44
"tests/library_checker_aizu_tests/convolution/min_plus_convolution.test.cpp": "2024-11-17 14:04:03 -0600",
5+
"tests/library_checker_aizu_tests/convolution/xor_convolution.test.cpp": "2026-03-06 12:02:07 -0700",
56
"tests/library_checker_aizu_tests/data_structures/binary_search_example.test.cpp": "2024-11-18 10:51:39 -0600",
67
"tests/library_checker_aizu_tests/data_structures/binary_trie.test.cpp": "2026-01-18 02:20:40 +0000",
78
"tests/library_checker_aizu_tests/data_structures/bit.test.cpp": "2026-01-23 04:31:29 +0000",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
#include "../math/mod_division.hpp"
3+
//! https://codeforces.com/blog/entry/127823
4+
//! @code
5+
//! vi c = xor_conv(a, b);
6+
//! // must have sz(a) == sz(b) == a power of 2
7+
//! // c[k] = sum of a[i]*b[j] where i^j==k
8+
//! @endcode
9+
//! @time O(n log n)
10+
//! @space O(n)
11+
void fwht(int n, vi& a) {
12+
for (int i = 1; i < n; i *= 2)
13+
for (int j = 0; j < n; j += 2 * i) rep(k, 0, i) {
14+
int x = a[j + k], y = a[i + j + k];
15+
a[j + k] = (x + y) % mod;
16+
a[i + j + k] = (x - y + mod) % mod;
17+
}
18+
}
19+
vi xor_conv(vi& a, vi& b) {
20+
int n = sz(a), inv = mod_div(1, n);
21+
fwht(n, a);
22+
fwht(n, b);
23+
vi res(n);
24+
rep(i, 0, n) res[i] = 1LL * a[i] * b[i] % mod;
25+
fwht(n, res);
26+
rep(i, 0, n) res[i] = 1LL * res[i] * inv % mod;
27+
return res;
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/bitwise_xor_convolution"
3+
#include "../template.hpp"
4+
#include "../../../library/convolution/xor_convolution.hpp"
5+
istream& operator>>(istream& is, vector<int>& v) {
6+
for (int i = 0; i < sz(v); i++) is >> v[i];
7+
return is;
8+
}
9+
int main() {
10+
cin.tie(0)->sync_with_stdio(0);
11+
int n;
12+
cin >> n;
13+
vi a(1 << n), b(1 << n);
14+
cin >> a >> b;
15+
vi c = xor_conv(a, b);
16+
for (int i = 0; i < (1 << n); i++) cout << c[i] << ' ';
17+
cout << '\n';
18+
return 0;
19+
}

0 commit comments

Comments
 (0)