cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 添え字 xor での畳み込み
(include/emthrm/math/convolution/xor_convolution.hpp)

$C_k = \sum_{k = i \circ j} A_i B_j$ を求める。ただし $\circ$ は二項演算である。

添え字 xor での畳み込みには『高速ウォルシュ・アダマール変換 (fast Walsh-Hadamard transform)』を用いる。

時間計算量

$O(N\log{N})$

仕様

添え字 and での畳み込み

名前 戻り値
template <typename T>
std::vector<T> and_convolution(std::vector<T> a, std::vector<T> b, const T id = 0);
$A, B$ に対する添え字 and での畳み込み

添え字 or での畳み込み

名前 戻り値
template <typename T>
std::vector<T> or_convolution(std::vector<T> a, std::vector<T> b, const T id = 0);
$A, B$ に対する添え字 or での畳み込み

添え字 xor での畳み込み

名前 戻り値
template <typename T>
std::vector<T> xor_convolution(std::vector<T> a, std::vector<T> b, const T id = 0);
$A, B$ に対する添え字 xor での畳み込み

添え字 gcd での畳み込み

名前 戻り値
template <typename T>
std::vector<T> gcd_convolution(std::vector<T> a, std::vector<T> b);
$A, B$ に対する添え字 gcd での畳み込み

添え字 lcm での畳み込み

名前 戻り値
template <typename T>
std::vector<T> lcm_convolution(std::vector<T> a, std::vector<T> b, const int n = -1);
$A, B$ に対する添え字 lcm での畳み込み

参考文献

高速ウォルシュ・アダマール変換

添え字 gcd での畳み込み

添え字 lcm での畳み込み

TODO

Submissons

Verified with

Code

#ifndef EMTHRM_MATH_CONVOLUTION_XOR_CONVOLUTION_HPP_
#define EMTHRM_MATH_CONVOLUTION_XOR_CONVOLUTION_HPP_

#include <algorithm>
#include <bit>
#include <vector>

namespace emthrm {

template <typename T>
std::vector<T> xor_convolution(std::vector<T> a, std::vector<T> b,
                               const T id = 0) {
  const int n = std::bit_ceil(std::max(a.size(), b.size()));
  const auto fast_walsh_hadamard_transform = [n](std::vector<T>* v) -> void {
    for (int i = 1; i < n; i <<= 1) {
      for (int s = 0; s < n; ++s) {
        if (s & i) continue;
        const T tmp1 = (*v)[s], tmp2 = (*v)[s | i];
        (*v)[s] = tmp1 + tmp2;
        (*v)[s | i] = tmp1 - tmp2;
      }
    }
  };
  a.resize(n, id);
  fast_walsh_hadamard_transform(&a);
  b.resize(n, id);
  fast_walsh_hadamard_transform(&b);
  for (int i = 0; i < n; ++i) {
    a[i] *= b[i];
  }
  fast_walsh_hadamard_transform(&a);
  for (int i = 0; i < n; ++i) {
    a[i] /= n;
  }
  return a;
}

}  // namespace emthrm

#endif  // EMTHRM_MATH_CONVOLUTION_XOR_CONVOLUTION_HPP_
#line 1 "include/emthrm/math/convolution/xor_convolution.hpp"



#include <algorithm>
#include <bit>
#include <vector>

namespace emthrm {

template <typename T>
std::vector<T> xor_convolution(std::vector<T> a, std::vector<T> b,
                               const T id = 0) {
  const int n = std::bit_ceil(std::max(a.size(), b.size()));
  const auto fast_walsh_hadamard_transform = [n](std::vector<T>* v) -> void {
    for (int i = 1; i < n; i <<= 1) {
      for (int s = 0; s < n; ++s) {
        if (s & i) continue;
        const T tmp1 = (*v)[s], tmp2 = (*v)[s | i];
        (*v)[s] = tmp1 + tmp2;
        (*v)[s | i] = tmp1 - tmp2;
      }
    }
  };
  a.resize(n, id);
  fast_walsh_hadamard_transform(&a);
  b.resize(n, id);
  fast_walsh_hadamard_transform(&b);
  for (int i = 0; i < n; ++i) {
    a[i] *= b[i];
  }
  fast_walsh_hadamard_transform(&a);
  for (int i = 0; i < n; ++i) {
    a[i] /= n;
  }
  return a;
}

}  // namespace emthrm
Back to top page