Skip to content

Commit 4190129

Browse files
committed
Update polynomial exponentiation in FFT
1 parent f553dd5 commit 4190129

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

fft.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,18 @@ poly pmul(poly p, poly q) {
8080
}
8181

8282
poly ppow(poly p, int k, int mod) {
83-
if (k == 0) return {1};
84-
poly q = pmul(p, p);
85-
for (int& c : q)
86-
c %= mod;
87-
q = ppow(q, k >> 1, mod);
88-
if (k % 2) {
89-
q = pmul(q, p);
90-
for (int& c : q)
83+
poly ret = {1};
84+
for (int i = 0; (1 << i) <= k; i++) {
85+
if (k & (1 << i)) {
86+
ret = pmul(ret, p);
87+
for (int& c : ret)
88+
c %= mod;
89+
}
90+
p = pmul(p, p);
91+
for (int& c : p)
9192
c %= mod;
9293
}
93-
return q;
94+
return ret;
9495
}
9596

9697
string pprint(poly p) {

0 commit comments

Comments
 (0)