This documentation is automatically generated by online-judge-tools/verification-helper
#include "matrix/black_box_linear_algebra.hpp"
Black Box Linear Algebraは、行列を陽に与えずに、行列 $A$ とベクトル $v$ の積の演算を行う関数(black box) $Ax$ を与えることで各種の計算を行うものである。以下、 $A$ と ベクトル $v$ の積の計算量を $T(N)$ とする。
$N$ 次正方行列 $A$ の最小多項式を求める。 $O(N^2)$
$A^k b$ を求める。多項式乗算の計算量を $M(N)$ とすると、 $O(N^2 + M(N)\log{k} + N T(N))$
$N$ 次正方行列 $A$ の行列式を求める。 $O(N^2 + N T(N))$
#pragma once
#include <cassert>
#include <vector>
#include "../fps/berlekamp_massey.hpp"
#include "../fps/poly_mod_pow.hpp"
#include "../modint/base.hpp"
#include "../utility/random_number_generator.hpp"
namespace ebi {
template <Modint mint, class F>
std::vector<mint> matrix_minimum_poly(int n, F Ax) {
static random_number_generator rng;
std::vector<mint> s(2 * n + 10, 0), u(n), b(n);
for (int i = 0; i < n; i++) {
u[i] = rng.get(0, mint::mod());
b[i] = rng.get(0, mint::mod());
}
for (int i = 0; i < 2 * n + 10; i++) {
for (int j = 0; j < n; j++) {
s[i] += u[j] * b[j];
}
b = Ax(b);
}
auto c = berlekamp_massey(s);
std::reverse(c.begin(), c.end());
return c;
}
template <Modint mint, class F>
std::vector<mint> pow(int n, F Ax, const std::vector<mint> &b, long long k) {
assert(n == (int)b.size());
using FPS = FormalPowerSeries<mint>;
auto g = matrix_minimum_poly<mint>(n, Ax);
auto c = poly_mod_pow<mint>({0, 1}, k, g);
FPS res(n, 0), Ab = b;
for (int i = 0; i < (int)c.size(); i++) {
res += Ab * c[i];
Ab = FPS(Ax(Ab));
}
return res;
}
template <Modint mint, class F> mint det(int n, F Ax) {
static random_number_generator rng;
std::vector<mint> d(n);
mint r = 1;
for (int i = 0; i < n; i++) {
d[i] = rng.get(1, mint::mod());
r *= d[i];
}
auto ADx = [&](std::vector<mint> v) -> std::vector<mint> {
assert(n == (int)v.size());
for (int i = 0; i < n; i++) {
v[i] *= d[i];
}
return Ax(v);
};
auto f = matrix_minimum_poly<mint>(n, ADx);
mint res = ((int)f.size() == n + 1 ? f[0] : 0);
if (n % 2 == 1) res = -res;
return res / r;
}
} // namespace ebi
#line 2 "matrix/black_box_linear_algebra.hpp"
#include <cassert>
#include <vector>
#line 2 "fps/berlekamp_massey.hpp"
#include <algorithm>
#line 5 "fps/berlekamp_massey.hpp"
#line 2 "modint/base.hpp"
#include <concepts>
#include <iostream>
#include <utility>
namespace ebi {
template <class T>
concept Modint = requires(T a, T b) {
a + b;
a - b;
a * b;
a / b;
a.inv();
a.val();
a.pow(std::declval<long long>());
T::mod();
};
template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) {
long long x;
os >> x;
a = x;
return os;
}
template <Modint mint>
std::ostream &operator<<(std::ostream &os, const mint &a) {
return os << a.val();
}
} // namespace ebi
#line 7 "fps/berlekamp_massey.hpp"
namespace ebi {
template <Modint mint>
std::vector<mint> berlekamp_massey(const std::vector<mint> &s) {
std::vector<mint> C = {1}, B = {1};
int L = 0, m = 1;
mint b = 1;
for (int n = 0; n < (int)s.size(); n++) {
mint d = s[n];
for (int i = 1; i <= L; i++) {
d += s[n - i] * C[i];
}
if (d == 0) {
m++;
} else if (2 * L <= n) {
auto T = C;
mint f = d / b;
C.resize((int)B.size() + m);
for (int i = 0; i < (int)B.size(); i++) {
C[i + m] -= f * B[i];
}
L = n + 1 - L;
B = T;
b = d;
m = 1;
} else {
mint f = d / b;
for (int i = 0; i < (int)B.size(); i++) {
C[i + m] -= f * B[i];
}
m++;
}
}
return C;
}
} // namespace ebi
#line 2 "fps/poly_mod_pow.hpp"
#line 2 "fps/fps.hpp"
#line 5 "fps/fps.hpp"
#include <optional>
#line 7 "fps/fps.hpp"
#line 9 "fps/fps.hpp"
namespace ebi {
template <Modint mint> struct FormalPowerSeries : std::vector<mint> {
private:
using std::vector<mint>::vector;
using std::vector<mint>::vector::operator=;
using FPS = FormalPowerSeries;
public:
FormalPowerSeries(const std::vector<mint> &a) {
*this = a;
}
FPS operator+(const FPS &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const FPS &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const FPS &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const FPS &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS operator%(const FPS &rhs) const noexcept {
return FPS(*this) %= rhs;
}
FPS operator+(const mint &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const mint &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const mint &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const mint &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS &operator+=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] += rhs[i];
}
return *this;
}
FPS &operator-=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] -= rhs[i];
}
return *this;
}
FPS &operator*=(const FPS &);
FPS &operator/=(const FPS &rhs) noexcept {
int n = deg() - 1;
int m = rhs.deg() - 1;
if (n < m) {
*this = {};
return *this;
}
*this = (*this).rev() * rhs.rev().inv(n - m + 1);
(*this).resize(n - m + 1);
std::reverse((*this).begin(), (*this).end());
return *this;
}
FPS &operator%=(const FPS &rhs) noexcept {
*this -= *this / rhs * rhs;
shrink();
return *this;
}
FPS &operator+=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] += rhs;
return *this;
}
FPS &operator-=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] -= rhs;
return *this;
}
FPS &operator*=(const mint &rhs) noexcept {
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= rhs;
}
return *this;
}
FPS &operator/=(const mint &rhs) noexcept {
mint inv_rhs = rhs.inv();
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= inv_rhs;
}
return *this;
}
FPS operator>>(int d) const {
if (deg() <= d) return {};
FPS f = *this;
f.erase(f.begin(), f.begin() + d);
return f;
}
FPS operator<<(int d) const {
FPS f = *this;
f.insert(f.begin(), d, 0);
return f;
}
FPS operator-() const {
FPS g(this->size());
for (int i = 0; i < (int)this->size(); i++) g[i] = -(*this)[i];
return g;
}
FPS pre(int sz) const {
return FPS(this->begin(), this->begin() + std::min(deg(), sz));
}
FPS rev() const {
auto f = *this;
std::reverse(f.begin(), f.end());
return f;
}
FPS differential() const {
int n = deg();
FPS g(std::max(0, n - 1));
for (int i = 0; i < n - 1; i++) {
g[i] = (*this)[i + 1] * (i + 1);
}
return g;
}
FPS integral() const {
int n = deg();
FPS g(n + 1);
g[0] = 0;
if (n > 0) g[1] = 1;
auto mod = mint::mod();
for (int i = 2; i <= n; i++) g[i] = (-g[mod % i]) * (mod / i);
for (int i = 0; i < n; i++) g[i + 1] *= (*this)[i];
return g;
}
FPS inv(int d = -1) const {
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = (*this)[0].inv();
while (n < d) {
n <<= 1;
g = (g * 2 - g * g * this->pre(n)).pre(n);
}
g.resize(d);
return g;
}
FPS log(int d = -1) const {
assert((*this)[0].val() == 1);
if (d < 0) d = deg();
return ((*this).differential() * (*this).inv(d)).pre(d - 1).integral();
}
FPS exp(int d = -1) const {
assert((*this)[0].val() == 0);
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = 1;
while (n < d) {
n <<= 1;
g = (g * (this->pre(n) - g.log(n) + 1)).pre(n);
}
g.resize(d);
return g;
}
FPS pow(long long k, int d = -1) const {
assert(k >= 0);
int n = deg();
if (d < 0) d = n;
if (k == 0) {
FPS f(d);
if (d > 0) f[0] = 1;
return f;
}
int low = d;
for (int i = n - 1; i >= 0; i--)
if ((*this)[i] != 0) low = i;
if (low >= (d + k - 1) / k) return FPS(d, 0);
int offset = k * low;
mint c = (*this)[low];
FPS g(d - offset);
for (int i = 0; i < std::min(n - low, d - offset); i++) {
g[i] = (*this)[i + low];
}
g /= c;
g = g.pow_1(k);
return (g << offset) * c.pow(k);
}
FPS pow_1(mint k, int d = -1) const {
assert((*this)[0] == 1);
return ((*this).log(d) * k).exp(d);
}
FPS pow_newton(long long k, int d = -1) const {
assert(k >= 0);
const int n = deg();
if (d < 0) d = n;
if (k == 0) {
FPS f(d);
if (d > 0) f[0] = 1;
return f;
}
for (int i = 0; i < n; i++) {
if ((*this)[i] != 0) {
mint rev = (*this)[i].inv();
FPS f = (((*this * rev) >> i).log(d) * k).exp(d);
f *= (*this)[i].pow(k);
f = (f << (i * k)).pre(d);
if (f.deg() < d) f.resize(d);
return f;
}
if (i + 1 >= (d + k - 1) / k) break;
}
return FPS(d);
}
int deg() const {
return (*this).size();
}
void shrink() {
while ((!this->empty()) && this->back() == 0) this->pop_back();
}
int count_terms() const {
int c = 0;
for (int i = 0; i < deg(); i++) {
if ((*this)[i] != 0) c++;
}
return c;
}
std::optional<FPS> sqrt(int d = -1) const;
static FPS exp_x(int n) {
FPS f(n);
mint fact = 1;
for (int i = 1; i < n; i++) fact *= i;
f[n - 1] = fact.inv();
for (int i = n - 1; i >= 0; i--) f[i - 1] = f[i] * i;
return f;
}
void fft();
void ifft();
};
} // namespace ebi
#line 5 "fps/poly_mod_pow.hpp"
namespace ebi {
template <Modint mint>
FormalPowerSeries<mint> poly_mod_pow(FormalPowerSeries<mint> f, long long k,
const FormalPowerSeries<mint> &g) {
FormalPowerSeries<mint> res = {1};
while (k > 0) {
if (k & 1) {
res *= f;
res %= g;
res.shrink();
}
f *= f;
f %= g;
f.shrink();
k >>= 1;
}
return res;
}
} // namespace ebi
#line 2 "utility/random_number_generator.hpp"
#line 4 "utility/random_number_generator.hpp"
#include <cstdint>
#include <numeric>
#include <random>
#line 8 "utility/random_number_generator.hpp"
namespace ebi {
struct random_number_generator {
random_number_generator(int seed = -1) {
if (seed < 0) seed = rnd();
mt.seed(seed);
}
void set_seed(int seed) {
mt.seed(seed);
}
template <class T> T get(T a, T b) {
std::uniform_int_distribution<T> dist(a, b - 1);
return dist(mt);
}
std::vector<int> get_permutation(int n) {
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
std::shuffle(p.begin(), p.end(), mt);
return p;
}
private:
std::mt19937_64 mt;
std::random_device rnd;
};
} // namespace ebi
#line 10 "matrix/black_box_linear_algebra.hpp"
namespace ebi {
template <Modint mint, class F>
std::vector<mint> matrix_minimum_poly(int n, F Ax) {
static random_number_generator rng;
std::vector<mint> s(2 * n + 10, 0), u(n), b(n);
for (int i = 0; i < n; i++) {
u[i] = rng.get(0, mint::mod());
b[i] = rng.get(0, mint::mod());
}
for (int i = 0; i < 2 * n + 10; i++) {
for (int j = 0; j < n; j++) {
s[i] += u[j] * b[j];
}
b = Ax(b);
}
auto c = berlekamp_massey(s);
std::reverse(c.begin(), c.end());
return c;
}
template <Modint mint, class F>
std::vector<mint> pow(int n, F Ax, const std::vector<mint> &b, long long k) {
assert(n == (int)b.size());
using FPS = FormalPowerSeries<mint>;
auto g = matrix_minimum_poly<mint>(n, Ax);
auto c = poly_mod_pow<mint>({0, 1}, k, g);
FPS res(n, 0), Ab = b;
for (int i = 0; i < (int)c.size(); i++) {
res += Ab * c[i];
Ab = FPS(Ax(Ab));
}
return res;
}
template <Modint mint, class F> mint det(int n, F Ax) {
static random_number_generator rng;
std::vector<mint> d(n);
mint r = 1;
for (int i = 0; i < n; i++) {
d[i] = rng.get(1, mint::mod());
r *= d[i];
}
auto ADx = [&](std::vector<mint> v) -> std::vector<mint> {
assert(n == (int)v.size());
for (int i = 0; i < n; i++) {
v[i] *= d[i];
}
return Ax(v);
};
auto f = matrix_minimum_poly<mint>(n, ADx);
mint res = ((int)f.size() == n + 1 ? f[0] : 0);
if (n % 2 == 1) res = -res;
return res / r;
}
} // namespace ebi