Skip to content

Commit e3ff553

Browse files
committed
Added pow
1 parent 4c549b9 commit e3ff553

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

content/numerical/FFTPolynomial.h

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@
1212
typedef Mod num;
1313
typedef vector<num> poly;
1414
vector<Mod> conv(vector<Mod> a, vector<Mod> b) {
15-
auto res = convMod<mod>(vl(all(a)), vl(all(b)));
15+
// auto res = convMod<mod>(vl(all(a)), vl(all(b)));
16+
auto res = conv(vl(all(a)), vl(all(b)));
1617
return vector<Mod>(all(res));
1718
}
18-
poly &operator+=(poly &a, poly &b) {
19+
poly &operator+=(poly &a, const poly &b) {
1920
a.resize(max(sz(a), sz(b)));
20-
rep(i,0,sz(b)) a[i] = a[i] + b[i];
21+
rep(i, 0, sz(b)) a[i] = a[i] + b[i];
2122
return a;
2223
}
23-
poly &operator-=(poly &a, poly b) {
24+
poly &operator-=(poly &a, const poly &b) {
2425
a.resize(max(sz(a), sz(b)));
25-
rep(i,0,sz(b)) a[i] = a[i] - b[i];
26+
rep(i, 0, sz(b)) a[i] = a[i] - b[i];
2627
return a;
2728
}
28-
poly &operator*=(poly &a, poly &b) { return a = conv(a, b); }
29+
poly &operator*=(poly &a, const poly &b) { return a = conv(a, b); }
30+
poly operator*(poly a, const num b) {
31+
poly c = a;
32+
trav(i, c) i = i * b;
33+
return c;
34+
}
2935
#define OP(o, oe) \
3036
poly operator o(poly a, poly b) { \
3137
poly c = a; \
@@ -39,13 +45,13 @@ poly inverse(poly A) {
3945
B = modK(B * (poly({num(2)}) - A * B), 2 * sz(B));
4046
return modK(B, sz(A));
4147
}
42-
poly &operator/=(poly &a, poly &b) {
48+
poly &operator/=(poly &a, poly b) {
4349
if (sz(a) < sz(b))
4450
return a = {};
4551
int s = sz(a) - sz(b) + 1;
4652
reverse(all(a)), reverse(all(b));
4753
a.resize(s), b.resize(s);
48-
a *= inverse(b);
54+
a = a * inverse(b);
4955
a.resize(s), reverse(all(a));
5056
return a;
5157
}
@@ -70,7 +76,7 @@ poly integr(poly a) {
7076
if (a.empty())
7177
return {0};
7278
poly b(sz(a) + 1);
73-
rep(i,1,sz(b)) b[i] = a[i - 1] / num(i);
79+
rep(i, 1, sz(b)) b[i] = a[i - 1] / num(i);
7480
return b;
7581
}
7682
poly log(poly a) { return modK(integr(deriv(a) * inverse(a)), sz(a)); }
@@ -79,9 +85,21 @@ poly exp(poly a) {
7985
if (a.empty())
8086
return b;
8187
while (sz(b) < sz(a)) {
82-
b.resize(sz(b)*2);
88+
b.resize(sz(b) * 2);
8389
b *= (poly({num(1)}) + modK(a, sz(b)) - log(b));
84-
b.resize(sz(b)/2 + 1);
90+
b.resize(sz(b) / 2 + 1);
8591
}
8692
return modK(b, sz(a));
8793
}
94+
poly pow(poly a, ll m) {
95+
int p = 0; int n = sz(a);
96+
while (p < sz(a) && a[p].x == 0)
97+
++p;
98+
num j = a[p];
99+
a = {a.begin() + p, a.end()};
100+
a = a * (num(1) / j);
101+
a.resize(n);
102+
auto res = exp(log(a) * num(m)) * (j ^ m);
103+
res.insert(res.begin(), p*m, 0);
104+
return modK(res, n);
105+
}

0 commit comments

Comments
 (0)