-
Notifications
You must be signed in to change notification settings - Fork 4
/
fft_online_ntt.cpp
182 lines (153 loc) · 4.45 KB
/
fft_online_ntt.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// https://tanujkhattar.files.wordpress.com/2018/01/onlinefft1.pdf
// F[n] =n−1∑i=1F[i]∗G[n−i]
using cd = complex<double>;
const double PI = acos(-1);
void fft(vector<cd> & a, bool invert) {
int n = a.size();
for (int i = 1, j = 0; i < n; i++) {
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(a[i], a[j]);
}
for (int len = 2; len <= n; len <<= 1) {
double ang = 2 * PI / len * (invert ? -1 : 1);
cd wlen(cos(ang), sin(ang));
for (int i = 0; i < n; i += len) {
cd w(1);
for (int j = 0; j < len / 2; j++) {
cd u = a[i+j], v = a[i+j+len/2] * w;
a[i+j] = u + v;
a[i+j+len/2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (cd & x : a)
x /= n;
}
}
void multiply(vector<int> const& a, vector<int> const& b,vector<int> &result) {
vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
int n = 1;
while (n < a.size() + b.size())
n <<= 1;
fa.resize(n);
fb.resize(n);
fft(fa, false);
fft(fb, false);
for (int i = 0; i < n; i++)
fa[i] *= fb[i];
fft(fa, true);
result.resize(n,0);
for (int i = 0; i < n; i++)
result[i] = round(fa[i].real());
}
const int MOD = 663224321;
vector<int> onlineFFT(vector<int> arr,int mySize = -1){
vector<int> res;
int need;
if (mySize != -1)
need = mySize;
else
need = arr.size();
arr.resize(need + 5);
res.resize(need + 5); // as (i+1,i+2) which we are doing down
fill(res.begin(), res.end(), 0);
res[0] = 1;
for (ll i = 0; i <= need - 1;
i++) // we know value of res[i] ,so we'll update forward ones
{
// first update using first two elements of arr
res[i + 1] += (res[i] * arr[1]) % MOD;
if (res[i + 1] >= MOD)
res[i + 1] -= MOD;
res[i + 2] += (res[i] * arr[2]) % MOD;
if (res[i + 2] >= MOD)
res[i + 2] -= MOD;
// now go update using all blocks of size
// i%(2^j)==0,j>0,convolve(arr[2^j+1,2^(2*j)],res[i-2^j,i-1])
for (int pw = 2; (i % pw == 0) && ((pw + 1) < need) && i;pw = pw * 2) { // as if(2^j+1>n) no use...
vector<int> A, B, C;
copy(arr.begin() + pw + 1, arr.begin() + min(need - 1, 2 * pw) + 1,
back_inserter(A));
copy(res.begin() + i - pw, res.begin() + i, back_inserter(B));
multiply(A, B, C);
ll offset = i + 1;
ll lim = min((ll)C.size(), need - offset);
for (ll i = 0; i < lim; i++) {
res[i + offset] += C[i];
if (res[i + offset] >= MOD)
res[i + offset] -= MOD;
}
}
}
return res;
}
const int MOD = 663224321;
const int LG = 19;
const int maxn = (1<<LG);
const int G = 3;
int W[maxn>>1],invW[maxn>>1];
int power(int x,int y,int MOD = ::MOD){
int res = 1;
while(y>0){
if(y&1) res = (res*x)%MOD;
x = (x*x)%MOD;
y >>= 1;
}
return res;
}
void precompute_powers(){
W[0] = invW[0] = 1;
int base = power(G, (MOD-1)/maxn, MOD);
int inv_base = power(base, MOD-2, MOD);
for(int i = 1;i<maxn/2;i++){
W[i] = (W[i-1] * 1LL * base) % MOD;
invW[i] = (invW[i-1] * 1LL * inv_base) % MOD;
}
}
void ntt (vector<int> &a, bool invert) {
int n = (int) a.size();
for (int i=1, j=0; i<n; ++i) {
int bit = n >> 1;
for (; j>=bit; bit>>=1)
j -= bit;
j += bit;
if (i < j)
swap (a[i], a[j]);
}
for (int len=2; len<=n; len<<=1) {
for (int i=0; i<n; i+=len) {
int ind = 0, add = maxn/len;
for (int j=0; j<len/2; ++j) {
int u = a[i+j], v = (a[i+j+len/2] * 1LL * (invert?invW[ind]:W[ind])) % MOD;
a[i+j] = (u + v) % MOD;
a[i+j+len/2] = (u - v + MOD) % MOD;
ind += add;
}
}
}
if (invert){
int inv_n = power(n, MOD-2, MOD);
for (int i=0; i<n; ++i)
a[i] = (a[i] * 1LL * inv_n) % MOD;
}
}
void multiply(vector<int> const& a, vector<int> const& b,vector<int> &result) {
vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end());
int n = 1;
while (n < a.size() + b.size())
n <<= 1;
fa.resize(n);
fb.resize(n);
ntt(fa, false);
ntt(fb, false);
for (int i = 0; i < n; i++)
fa[i] = (fa[i]*fb[i])%MOD;
ntt(fa, true);
result.resize(n,0);
}