-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse-fps.hpp
69 lines (51 loc) · 1.69 KB
/
sparse-fps.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
#ifndef HARUILIB_FORMAL_POWER_SERIES_SPARSE_FPS_HPP
#define HARUILIB_FORMAL_POWER_SERIES_SPARSE_FPS_HPP
#include <vector>
using namespace std;
#include "formal-power-series.hpp"
// calculate inverse of f(sparse)
// deg : -1 + ( maximum degree of g )
template <typename mint>
FPS<mint> inv_sparse(const vector<pair<int,mint>>& f, int deg) {
assert(deg >= 0);
for(int i=0; i<(int)f.size()-1; i++) assert(f[i].first < f[i+1].first);
assert(f[0].first == 0 && f[0].second != mint(0));
mint f0inv = f[0].second.inv();
vector<mint> g(deg); g[0] = f0inv;
for(int i=0; i<deg-1; i++) {
for (pair<int,mint> pim : f) {
if (i+1 - pim.first >= 0) g[i+1] -= pim.second * g[i+1 - pim.first];
else continue;
}
g[i+1] *= f0inv;
}
return g;
}
template <typename mint>
FPS<mint> inv_sparse(const FPS<mint>& f, int deg) {
vector<pair<int,mint>> vpim;
for(int i=0; i<f.size(); i++) if (f[i] != mint(0)) vpim.emplace_back(i, f[i]);
return inv_sparse(vpim, deg);
}
/* tabun baggute masu. TODO
template<typename mint>
FPS multiply_sparse(const FPS& f, const vector<pair<int,mint>>& g, int deg) {
FPS ret(deg);
for (pair<int,mint> pim : g) {
assert(pim.second != 0);
if (pim.second == 0) continue;
for(int i=0; i<f.size(); i++) {
if (i+pim.first >= ret.size()) continue;
if (f[i] != mint(0) && pim.second != mint(0)) ret[i+pim.first] += pim.second * f[i];
}
}
return ret;
}
template <typename mint>
FPS multiply_sparse(const FPS& f, const FPS& g) {
vector<pair<mint,int>> vpmi;
for(int i=0; i<g.size(); i++) if (g[i] != mint(0)) vpmi.emplace_back(i, g[i]);
return multiply_sparse(f, vpmi);
}
*/
#endif // HARUILIB_FORMAL_POWER_SERIES_SPARSE_FPS_HPP