cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:warning: 数学/畳み込み/クロネッカー冪とベクトルの積
(test/math/convolution/kronecker_power-vector_multiplication.test.cpp)

Depends on

Code

/*
 * @title 数学/畳み込み/クロネッカー冪とベクトルの積
 *
 * verification-helper: IGNORE
 * verification-helper: PROBLEM https://atcoder.jp/contests/abc288/tasks/abc288_g
 */

#include <cmath>
#include <iostream>
#include <vector>

#include "emthrm/math/convolution/kronecker_power-vector_multiplication.hpp"
#include "emthrm/math/matrix/matrix.hpp"

int main() {
  int n;
  std::cin >> n;
  const int m = std::llround(std::pow(3, n));
  std::vector<int> a(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> a[i];
  }
  emthrm::Matrix<int> inv(3, 3);
  inv[0][1] = inv[1][0] = inv[1][2] = inv[2][1] = 1;
  inv[0][2] = inv[1][1] = inv[2][0] = -1;
  const std::vector<int> b =
      emthrm::kronecker_power_vector_multiplication(inv, a);
  for (int i = 0; i < m; ++i) {
    std::cout << b[i] << " \n"[i + 1 == m];
  }
  return 0;
}
#line 1 "test/math/convolution/kronecker_power-vector_multiplication.test.cpp"
/*
 * @title 数学/畳み込み/クロネッカー冪とベクトルの積
 *
 * verification-helper: IGNORE
 * verification-helper: PROBLEM https://atcoder.jp/contests/abc288/tasks/abc288_g
 */

#include <cmath>
#include <iostream>
#include <vector>

#line 1 "include/emthrm/math/convolution/kronecker_power-vector_multiplication.hpp"



#include <cassert>
#line 7 "include/emthrm/math/convolution/kronecker_power-vector_multiplication.hpp"

#line 1 "include/emthrm/math/matrix/matrix.hpp"



#line 5 "include/emthrm/math/matrix/matrix.hpp"

namespace emthrm {

template <typename T>
struct Matrix {
  explicit Matrix(const int m, const int n, const T def = 0)
      : data(m, std::vector<T>(n, def)) {}

  int nrow() const { return data.size(); }
  int ncol() const { return data.empty() ? 0 : data.front().size(); }

  Matrix pow(long long exponent) const {
    const int n = nrow();
    Matrix<T> res(n, n, 0), tmp = *this;
    for (int i = 0; i < n; ++i) {
      res[i][i] = 1;
    }
    for (; exponent > 0; exponent >>= 1) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
    }
    return res;
  }

  inline const std::vector<T>& operator[](const int i) const { return data[i]; }
  inline std::vector<T>& operator[](const int i) { return data[i]; }

  Matrix& operator=(const Matrix& x) = default;

  Matrix& operator+=(const Matrix& x) {
    const int m = nrow(), n = ncol();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        data[i][j] += x[i][j];
      }
    }
    return *this;
  }

  Matrix& operator-=(const Matrix& x) {
    const int m = nrow(), n = ncol();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        data[i][j] -= x[i][j];
      }
    }
    return *this;
  }

  Matrix& operator*=(const Matrix& x) {
    const int m = nrow(), l = ncol(), n = x.ncol();
    std::vector<std::vector<T>> res(m, std::vector<T>(n, 0));
    for (int i = 0; i < m; ++i) {
      for (int k = 0; k < l; ++k) {
        for (int j = 0; j < n; ++j) {
          res[i][j] += data[i][k] * x[k][j];
        }
      }
    }
    data.swap(res);
    return *this;
  }

  Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
  Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
  Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }

 private:
  std::vector<std::vector<T>> data;
};

}  // namespace emthrm


#line 9 "include/emthrm/math/convolution/kronecker_power-vector_multiplication.hpp"

namespace emthrm {

template <typename T>
std::vector<T> kronecker_power_vector_multiplication(const Matrix<T>& g,
                                                     std::vector<T> v) {
  const int d = g.nrow(), n = v.size();
  assert(std::llround(std::pow(d, std::log(n) / std::log(d))) == n);
  Matrix<T> tmp(d, 1);
  for (int block = 1; block < n; block *= d) {
    for (int i = 0; i < n; i += block * d) {
      for (int j = 0; j < block; ++j) {
        for (int x = 0; x < d; ++x) {
          tmp[x][0] = v[i + j + block * x];
        }
        tmp = g * tmp;
        for (int x = 0; x < d; ++x) {
          v[i + j + block * x] = tmp[x][0];
        }
      }
    }
  }
  return v;
}

}  // namespace emthrm


#line 14 "test/math/convolution/kronecker_power-vector_multiplication.test.cpp"

int main() {
  int n;
  std::cin >> n;
  const int m = std::llround(std::pow(3, n));
  std::vector<int> a(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> a[i];
  }
  emthrm::Matrix<int> inv(3, 3);
  inv[0][1] = inv[1][0] = inv[1][2] = inv[2][1] = 1;
  inv[0][2] = inv[1][1] = inv[2][0] = -1;
  const std::vector<int> b =
      emthrm::kronecker_power_vector_multiplication(inv, a);
  for (int i = 0; i < m; ++i) {
    std::cout << b[i] << " \n"[i + 1 == m];
  }
  return 0;
}
Back to top page