We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f553dd5 commit 4190129Copy full SHA for 4190129
fft.cpp
@@ -80,17 +80,18 @@ poly pmul(poly p, poly q) {
80
}
81
82
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
+ poly ret = {1};
+ for (int i = 0; (1 << i) <= k; i++) {
+ if (k & (1 << i)) {
+ ret = pmul(ret, p);
+ for (int& c : ret)
+ c %= mod;
+ }
+ p = pmul(p, p);
91
+ for (int& c : p)
92
c %= mod;
93
- return q;
94
+ return ret;
95
96
97
string pprint(poly p) {
0 commit comments