-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbla.hpp
95 lines (91 loc) · 2.6 KB
/
bbla.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include "FPS/berlekampmassey.hpp"
#include "Utility/random.hpp"
template <typename T> Poly<T> RandPoly(int n) {
Poly<T> ret(n);
for (auto &x : ret)
x = Random::get(1, T::get_mod() - 1);
return ret;
}
template <typename T> struct SparseMatrix {
vector<T> base;
vector<map<int, T>> extra;
SparseMatrix(int n, T v = 0) : base(n, v), extra(n) {}
int size() const { return base.size(); }
inline void add(int i, int j, T x) { extra[i][j] += x; }
friend Poly<T> operator*(const SparseMatrix<T> &A, const Poly<T> &b) {
int n = A.size();
Poly<T> ret(n);
T sum;
for (auto &v : b)
sum += v;
rep(i, 0, n) {
T add = sum;
for (auto &[j, v] : A.extra[i]) {
ret[i] += v * b[j];
add -= b[j];
}
ret[i] += add * A.base[i];
}
return ret;
}
void mul(int i, T x) {
base[i] *= x;
for (auto &[_, v] : extra[i])
v *= x;
}
};
template <typename T> Poly<T> MinPolyforVector(const vector<Poly<T>> &b) {
int n = b.size(), m = b[0].size();
Poly<T> base = RandPoly<T>(m), a(n);
rep(i, 0, n) rep(j, 0, m) a[i] += base[j] * b[i][j];
return Poly<T>(BerlekampMassey(a)).rev();
}
template <typename T> Poly<T> MinPolyforMatrix(const SparseMatrix<T> &A) {
int n = A.size();
Poly<T> base = RandPoly<T>(n);
vector<Poly<T>> b(n * 2 + 1);
rep(i, 0, n * 2 + 1) b[i] = base, base = A * base;
return MinPolyforVector(b);
}
template <typename T>
Poly<T> FastPow(const SparseMatrix<T> &A, Poly<T> b, ll t) {
int n = A.size();
auto mp = MinPolyforMatrix(A).rev();
Poly<T> cs({T(1)}), base({T(0), T(1)});
while (t) {
if (t & 1) {
cs *= base;
cs %= mp;
}
base = base.square();
base %= mp;
t >>= 1;
}
Poly<T> ret(n);
for (auto &c : cs)
ret += b * c, b = A * b;
return ret;
}
template <typename T> T FastDet(const SparseMatrix<T> &A) {
int n = A.size();
for (;;) {
Poly<T> d = RandPoly<T>(n);
SparseMatrix<T> AD = A;
rep(i, 0, n) AD.mul(i, d[i]);
auto mp = MinPolyforMatrix(AD);
if (mp.back() == 0)
return 0;
if (int(mp.size()) != n + 1)
continue;
T ret = mp.back(), base = 1;
if (n & 1)
ret = -ret;
for (auto &v : d)
base *= v;
return ret / base;
}
}
/**
* @brief Black Box Linear Algebra
*/