競技プログラミング用 C++ ライブラリ
convolution/bitwise_convolution.hpp · GitHub で見る
共通ページ
#include "lib/convolution/bitwise_convolution.hpp"
std::vector<long long> a{1, 2, 3, 4}, b{5, 6, 7, 8};
auto c = and_convolution(a, b); // {103,52,73,32}
auto d = or_convolution(a, b); // {5,28,43,184}
auto f = xor_convolution(a, b); // {70,68,62,60}
| 関数 | 定義 | 計算量 |
|---|---|---|
and_convolution(a, b) |
\(\displaystyle c_k = \sum_{i\mathbin{\&}j=k} a_i b_j\) | \(O(N\log N)\) |
or_convolution(a, b) |
\(\displaystyle c_k = \sum_{i\mathbin{\vert}j=k} a_i b_j\) | \(O(N\log N)\) |
xor_convolution(a, b) |
\(\displaystyle c_k = \sum_{i\mathbin{\oplus}j=k} a_i b_j\) | \(O(N\log N)\) |
添字 i,j に AND・OR・XOR を適用し、その添字へ積 a[i]*b[j] を加えます。入力を変更せず、新しい配列を返します。短い配列の不足分は 0 で埋めます。どちらかが空なら空配列です。
長い方の長さを 2 の冪に切り上げた \(N\) 個を返します。係数は環の要素、XOR ではさらに \(N\) で割れる型を使います。整数型では中間値も型の範囲内とします。
bitwise_zeta<inverse, superset>(a) はその場でゼータ/メビウス変換します。既定は <false, false>、長さは 2 の冪です。walsh_hadamard(a) は非正規化の Hadamard 変換です。
時間 \(O(N \log N)\)、追加空間 \(O(N)\) です。変換のみの追加空間は \(O(1)\) です。
#ifndef BITWISE_CONVOLUTION_HPP
#define BITWISE_CONVOLUTION_HPP
#include "../components/bitwise_zeta.hpp"
#include "../components/walsh_hadamard.hpp"
#include "../components/bitwise_convolution_modular.hpp"
#include <type_traits>
#include <algorithm>
#include <bit>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <utility>
#include <vector>
namespace bitwise_convolution_detail {
template <int kind, class T>
std::vector<T> multiply(std::vector<T> a, std::vector<T> b) {
if (a.empty() || b.empty()) return {};
std::size_t n = std::bit_ceil(std::max(a.size(), b.size()));
a.resize(n, T(0));
b.resize(n, T(0));
#if (defined(__x86_64__) || defined(__i386__)) && !defined(BITWISE_CONVOLUTION_DISABLE_AVX2)
if constexpr (std::is_same_v<T, mint>) {
if (n >= 8 && Modular::vectorized()) {
Modular::multiply<kind>(a, b);
return a;
}
}
#endif
if constexpr (kind == 2) {
walsh_hadamard(a);
walsh_hadamard(b);
} else {
bitwise_zeta<false, kind == 0>(a);
bitwise_zeta<false, kind == 0>(b);
}
for (std::size_t i = 0; i < n; ++i) a[i] *= b[i];
if constexpr (kind == 2) {
walsh_hadamard(a);
if constexpr (std::numeric_limits<T>::is_integer)
for (T &x : a) x /= std::int64_t(n);
else {
T inverse = T(1) / T(n);
for (T &x : a) x *= inverse;
}
} else bitwise_zeta<true, kind == 0>(a);
return a;
}
}
template <class T>
std::vector<T> and_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<0>(std::move(a), std::move(b));
}
template <class T>
std::vector<T> or_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<1>(std::move(a), std::move(b));
}
template <class T>
std::vector<T> xor_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<2>(std::move(a), std::move(b));
}
#endif
#line 1 "convolution/bitwise_convolution.hpp"
#line 1 "components/bitwise_zeta.hpp"
#include <bit>
#include <cassert>
#include <cstddef>
#include <vector>
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace bitwise_zeta_detail {
template <bool inverse, bool superset, class T>
void stages(std::vector<T> &a, std::size_t start, std::size_t n, std::size_t half) {
auto step = [&](std::size_t low, std::size_t high) {
T &to = a[superset ? low : high];
const T &from = a[superset ? high : low];
if constexpr (inverse) to -= from;
else to += from;
};
for (; half < n / 2; half *= 4)
for (std::size_t block = start; block < start + n; block += 4 * half)
for (std::size_t j = block; j < block + half; ++j) {
step(j, j + half);
step(j + 2 * half, j + 3 * half);
step(j, j + 2 * half);
step(j + half, j + 3 * half);
}
if (half < n)
for (std::size_t j = start; j < start + half; ++j) step(j, j + half);
}
}
template <bool inverse = false, bool superset = false, class T>
void bitwise_zeta(std::vector<T> &a) {
assert(a.empty() || std::has_single_bit(a.size()));
constexpr std::size_t block = 1024;
if (a.size() <= block) bitwise_zeta_detail::stages<inverse, superset>(a, 0, a.size(), 1);
else {
for (std::size_t start = 0; start < a.size(); start += block)
bitwise_zeta_detail::stages<inverse, superset>(a, start, block, 1);
bitwise_zeta_detail::stages<inverse, superset>(a, 0, a.size(), block);
}
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 1 "components/walsh_hadamard.hpp"
#line 8 "components/walsh_hadamard.hpp"
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace walsh_hadamard_detail {
template <class T>
void stages(std::vector<T> &a, std::size_t start, std::size_t n, std::size_t half) {
auto step = [&](std::size_t low, std::size_t high) {
T x = a[low], y = a[high];
a[low] = x + y;
a[high] = x - y;
};
for (; half < n / 2; half *= 4)
for (std::size_t block = start; block < start + n; block += 4 * half)
for (std::size_t j = block; j < block + half; ++j) {
step(j, j + half);
step(j + 2 * half, j + 3 * half);
step(j, j + 2 * half);
step(j + half, j + 3 * half);
}
if (half < n)
for (std::size_t j = start; j < start + half; ++j) step(j, j + half);
}
}
template <class T>
void walsh_hadamard(std::vector<T> &a) {
assert(a.empty() || std::has_single_bit(a.size()));
constexpr std::size_t block = 1024;
if (a.size() <= block) walsh_hadamard_detail::stages(a, 0, a.size(), 1);
else {
for (std::size_t start = 0; start < a.size(); start += block)
walsh_hadamard_detail::stages(a, start, block, 1);
walsh_hadamard_detail::stages(a, 0, a.size(), block);
}
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 1 "components/bitwise_convolution_modular.hpp"
#include <algorithm>
#line 7 "components/bitwise_convolution_modular.hpp"
#include <type_traits>
#line 1 "components/mint_arithmetic.hpp"
#line 1 "math/mint.hpp"
#line 4 "math/mint.hpp"
#include <istream>
#include <ostream>
#line 7 "math/mint.hpp"
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
template <class I>
constexpr bool mint_integral = std::is_integral_v<I>
#if defined(__SIZEOF_INT128__)
|| std::is_same_v<I, __int128> || std::is_same_v<I, unsigned __int128>
#endif
;
struct [[gnu::warn_unused]] Mint {
using mint = Mint;
unsigned v = 0;
[[nodiscard]] static constexpr unsigned mod() { return 998244353; }
[[nodiscard, gnu::always_inline]] static unsigned reduce(unsigned long long x) {
return (x + (unsigned long long)(unsigned(x) * 998244351u) * mod()) >> 32;
}
[[gnu::always_inline]] Mint() = default;
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
Mint(I x) {
long long r = x % (long long)mod();
if (r < 0) r += mod();
v = reduce((unsigned long long)r * 932051910);
}
[[nodiscard, gnu::always_inline]] static Mint raw(unsigned x) {
Mint a;
a.v = reduce((unsigned long long)x * 932051910);
return a;
}
[[nodiscard, gnu::always_inline]] static Mint word(unsigned x) {
Mint a;
a.v = x;
return a;
}
[[nodiscard, gnu::always_inline]] unsigned val() const {
unsigned r = reduce(v);
return r < mod() ? r : r - mod();
}
friend std::istream &operator>>(std::istream &is, Mint &x) {
long long value;
if (is >> value) x = Mint(value);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Mint &x) {
return os << x.val();
}
[[gnu::always_inline]] Mint &operator+=(Mint b) {
v += b.v;
if (v >= 2 * mod()) v -= 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator-=(Mint b) {
v -= b.v;
if (v >> 31) v += 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator*=(Mint b) {
v = reduce((unsigned long long)v * b.v);
return *this;
}
Mint &operator/=(Mint b) { return *this *= b.inv(); }
Mint &operator++() { return *this += Mint(1); }
Mint &operator--() { return *this -= Mint(1); }
Mint operator++(int) {
Mint a = *this;
++*this;
return a;
}
Mint operator--(int) {
Mint a = *this;
--*this;
return a;
}
[[nodiscard, gnu::always_inline]] Mint operator+() const { return *this; }
[[nodiscard, gnu::always_inline]] Mint operator-() const { return word(v ? 2 * mod() - v : 0); }
[[nodiscard, gnu::always_inline]] Mint operator+(Mint b) const { return Mint(*this) += b; }
[[nodiscard, gnu::always_inline]] Mint operator-(Mint b) const { return Mint(*this) -= b; }
[[nodiscard, gnu::always_inline]] Mint operator*(Mint b) const { return Mint(*this) *= b; }
[[nodiscard]] Mint operator/(Mint b) const { return Mint(*this) /= b; }
[[nodiscard, gnu::always_inline]] bool operator==(Mint b) const { return (v < mod() ? v : v - mod()) == (b.v < mod() ? b.v : b.v - mod()); }
[[nodiscard, gnu::always_inline]] bool operator!=(Mint b) const { return !(*this == b); }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator+(I a, Mint b) { return Mint(a) + b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator-(I a, Mint b) { return Mint(a) - b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator*(I a, Mint b) { return Mint(a) * b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator/(I a, Mint b) { return Mint(a) / b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator==(I a, Mint b) { return Mint(a) == b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator!=(I a, Mint b) { return Mint(a) != b; }
[[nodiscard]] Mint pow(long long n) const {
assert(n >= 0);
if (!n) return Mint(1);
Mint r(1), a = *this;
for (; n > 1; n >>= 1, a *= a)
if (n & 1) r *= a;
return r * a;
}
[[nodiscard, gnu::noinline]] Mint inv() const {
assert(*this != Mint(0));
Mint a2 = *this * *this, a3 = a2 * *this;
Mint a7 = a3 * a3 * *this;
Mint a31 = a7;
for (int i = 0; i < 2; ++i) a31 *= a31;
a31 *= a3;
Mint r = a7;
for (int i = 0; i < 3; ++i) r *= r;
r *= a3;
r *= r;
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 5; ++i) r *= r;
r *= a31;
}
for (int i = 0; i < 3; ++i) r *= r;
return r * a7;
}
};
} // namespace mint_detail
using mint = mint_detail::Mint;
using fps_mint = mint;
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 5 "components/mint_arithmetic.hpp"
#if defined(__x86_64__) || defined(__i386__)
#include <immintrin.h>
#endif
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
struct Ops {
using T = mint;
typedef unsigned int U __attribute__((may_alias));
static constexpr U mod = 998244353, twice = 2 * mod, one = 301989884;
static U reduce(unsigned long long x) { return T::reduce(x); }
static U mul(U a, U b) { return reduce((unsigned long long)a * b); }
static U add(U a, U b) {
U c = a + b;
return c < twice ? c : c - twice;
}
static U sub(U a, U b) {
U c = a - b;
return c < twice ? c : c + twice;
}
static U in(U a) { return mul(a, 932051910); }
static U out(U a) {
a = reduce(a);
return a < mod ? a : a - mod;
}
static U quotient(U r) { return ((unsigned long long)r << 32) / mod; }
static U fixed_mul(U a, U r, U q) {
return a * r - U(((unsigned long long)a * q) >> 32) * mod;
}
#if defined(__x86_64__) || defined(__i386__)
static bool vectorized() {
#if defined(__AVX2__)
return true;
#else
static const bool available = __builtin_cpu_supports("avx2");
return available;
#endif
}
using I8 = __m256i;
__attribute__((target("avx2"))) static I8 load8(const U *a) {
return _mm256_loadu_si256(reinterpret_cast<const I8 *>(a));
}
__attribute__((target("avx2"))) static void store8(U *a, I8 x) {
_mm256_storeu_si256(reinterpret_cast<I8 *>(a), x);
}
__attribute__((target("avx2"), always_inline)) static inline I8 reduce8(I8 x, I8 y) {
I8 r = _mm256_set1_epi32(998244351), p = _mm256_set1_epi32(mod);
x = _mm256_add_epi64(x, _mm256_mul_epu32(_mm256_mul_epu32(x, r), p));
y = _mm256_add_epi64(y, _mm256_mul_epu32(_mm256_mul_epu32(y, r), p));
return _mm256_blend_epi32(_mm256_srli_epi64(x, 32), y, 0xaa);
}
__attribute__((target("avx2"))) static I8 mul8(I8 a, I8 b) {
return reduce8(_mm256_mul_epu32(a, b), _mm256_mul_epu32(_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32)));
}
__attribute__((target("avx2"))) static I8 fixed_mul8(I8 a, I8 r, I8 q) {
I8 x = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
I8 y = _mm256_mul_epu32(_mm256_srli_epi64(a, 32), q);
I8 t = _mm256_blend_epi32(x, y, 0xaa);
return _mm256_sub_epi32(_mm256_mullo_epi32(a, r),
_mm256_mullo_epi32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 fixed_even8(I8 a, I8 r, I8 q) {
I8 t = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
return _mm256_sub_epi64(_mm256_mul_epu32(a, r),
_mm256_mul_epu32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 add8(I8 a, I8 b) {
I8 x = _mm256_add_epi32(a, b);
return _mm256_min_epu32(x, _mm256_sub_epi32(x, _mm256_set1_epi32(twice)));
}
__attribute__((target("avx2"))) static I8 sub8(I8 a, I8 b) {
I8 x = _mm256_sub_epi32(a, b);
return _mm256_min_epu32(x, _mm256_add_epi32(x, _mm256_set1_epi32(twice)));
}
#endif
};
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 9 "components/bitwise_convolution_modular.hpp"
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace bitwise_convolution_detail {
struct Modular : mint_detail::Ops {
static_assert(sizeof(mint) == sizeof(U) && std::is_trivially_copyable_v<mint>);
#if defined(__x86_64__) || defined(__i386__)
__attribute__((target("avx2"))) static I8 load8(const mint *a) {
return mint_detail::Ops::load8(reinterpret_cast<const U *>(a));
}
__attribute__((target("avx2"))) static void store8(mint *a, I8 value) {
mint_detail::Ops::store8(reinterpret_cast<U *>(a), value);
}
template <int kind, bool inverse>
__attribute__((target("avx2"))) static void pair(I8 &x, I8 &y) {
if constexpr (kind == 2) {
I8 old = x;
x = add8(x, y), y = sub8(old, y);
} else if constexpr (kind == 0) x = inverse ? sub8(x, y) : add8(x, y);
else y = inverse ? sub8(y, x) : add8(y, x);
}
template <int kind, bool inverse, int mask>
__attribute__((target("avx2"))) static I8 lanes(I8 x, I8 y) {
if constexpr (kind == 2) return _mm256_blend_epi32(add8(x, y), sub8(y, x), mask);
else return _mm256_blend_epi32(x, inverse ? sub8(x, y) : add8(x, y), mask);
}
template <int kind, bool inverse>
__attribute__((target("avx2"))) static void layers(mint *a, std::size_t start, std::size_t n, std::size_t half) {
for (; half < n / 4; half *= 8)
for (std::size_t block = start; block < start + n; block += 8 * half)
for (std::size_t i = block; i < block + half; i += 8) {
I8 x[8];
#pragma GCC unroll 8
for (int j = 0; j < 8; ++j) x[j] = load8(a + i + j * half);
#pragma GCC unroll 3
for (int h = 1; h <= 4; h *= 2)
#pragma GCC unroll 8
for (int j = 0; j < 8; ++j) if (!(j & h)) pair<kind, inverse>(x[j], x[j + h]);
#pragma GCC unroll 8
for (int j = 0; j < 8; ++j) store8(a + i + j * half, x[j]);
}
for (; half < n / 2; half *= 4)
for (std::size_t block = start; block < start + n; block += 4 * half)
for (std::size_t i = block; i < block + half; i += 8) {
I8 x = load8(a + i), y = load8(a + i + half), z = load8(a + i + 2 * half), w = load8(a + i + 3 * half);
pair<kind, inverse>(x, y), pair<kind, inverse>(z, w);
pair<kind, inverse>(x, z), pair<kind, inverse>(y, w);
store8(a + i, x), store8(a + i + half, y), store8(a + i + 2 * half, z), store8(a + i + 3 * half, w);
}
if (half < n)
for (std::size_t i = start; i < start + half; i += 8) {
I8 x = load8(a + i), y = load8(a + i + half);
pair<kind, inverse>(x, y);
store8(a + i, x), store8(a + i + half, y);
}
}
template <int kind, bool inverse>
__attribute__((target("avx2"))) static void transform(mint *a, std::size_t n) {
constexpr std::size_t block = 1024;
for (std::size_t start = 0; start < n; start += block) {
std::size_t size = std::min(block, n - start);
for (std::size_t i = start; i < start + size; i += 8) {
I8 x = load8(a + i);
x = lanes<kind, inverse, kind == 0 ? 0x55 : 0xaa>(x, _mm256_shuffle_epi32(x, 0xb1));
x = lanes<kind, inverse, kind == 0 ? 0x33 : 0xcc>(x, _mm256_shuffle_epi32(x, 0x4e));
x = lanes<kind, inverse, kind == 0 ? 0x0f : 0xf0>(x, _mm256_permute2x128_si256(x, x, 1));
store8(a + i, x);
}
layers<kind, inverse>(a, start, size, 8);
}
if (n > block) layers<kind, inverse>(a, 0, n, block);
}
template <int kind>
__attribute__((target("avx2"))) static void multiply(std::vector<mint> &a, std::vector<mint> &b) {
std::size_t n = a.size();
auto *x = a.data(), *y = b.data();
transform<kind, false>(x, n), transform<kind, false>(y, n);
I8 factor{};
if constexpr (kind == 2) factor = _mm256_set1_epi32(mint(n).inv().v);
for (std::size_t i = 0; i < n; i += 8) {
I8 value = mul8(load8(x + i), load8(y + i));
if constexpr (kind == 2) value = mul8(value, factor);
store8(x + i, value);
}
transform<kind, true>(x, n);
}
#endif
};
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 12 "convolution/bitwise_convolution.hpp"
#include <cstdint>
#include <limits>
#include <utility>
#line 16 "convolution/bitwise_convolution.hpp"
namespace bitwise_convolution_detail {
template <int kind, class T>
std::vector<T> multiply(std::vector<T> a, std::vector<T> b) {
if (a.empty() || b.empty()) return {};
std::size_t n = std::bit_ceil(std::max(a.size(), b.size()));
a.resize(n, T(0));
b.resize(n, T(0));
#if (defined(__x86_64__) || defined(__i386__)) && !defined(BITWISE_CONVOLUTION_DISABLE_AVX2)
if constexpr (std::is_same_v<T, mint>) {
if (n >= 8 && Modular::vectorized()) {
Modular::multiply<kind>(a, b);
return a;
}
}
#endif
if constexpr (kind == 2) {
walsh_hadamard(a);
walsh_hadamard(b);
} else {
bitwise_zeta<false, kind == 0>(a);
bitwise_zeta<false, kind == 0>(b);
}
for (std::size_t i = 0; i < n; ++i) a[i] *= b[i];
if constexpr (kind == 2) {
walsh_hadamard(a);
if constexpr (std::numeric_limits<T>::is_integer)
for (T &x : a) x /= std::int64_t(n);
else {
T inverse = T(1) / T(n);
for (T &x : a) x *= inverse;
}
} else bitwise_zeta<true, kind == 0>(a);
return a;
}
}
template <class T>
std::vector<T> and_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<0>(std::move(a), std::move(b));
}
template <class T>
std::vector<T> or_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<1>(std::move(a), std::move(b));
}
template <class T>
std::vector<T> xor_convolution(std::vector<T> a, std::vector<T> b) {
return bitwise_convolution_detail::multiply<2>(std::move(a), std::move(b));
}