Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4191 [CTSC2010]性能优化

P4191 [CTSC2010]性能优化

循环卷积模板题。由于单位根的性质,将 FFT 中的点值换为 $\omega _ n$ 的幂即可实现循环卷积。可以 Chirp Z 变换 加速。

在这道题中,单位根 $\omega _ n = g ^ {\frac {mod - 1} n} = g$ 。原根的判定方法是对于所有 $mod - 1$ 的因数 $i$ 都满足 $k ^ {\frac {mod - 1} i} \neq 1$ 。

查看代码
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
#include <cstdio>
#include <vector>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef __int128 L;
typedef long long LL;
const int N = 3e6 + 10;
int w, iw, mod, g[2][N];
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void init ()
{
vector <int> p;
auto check = [&](int k)
{
for (int i : p)
if (binpow(k, (mod - 1) / i) == 1)
return false;
return true;
};
int x = mod - 1;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
{
p.push_back(i);
while (x % i == 0) x /= i;
}
if (x > 1) p.push_back(x);
for (int i = 2; i < mod; ++i)
if (check(i)) { w = i; break; }
iw = binpow(w);
g[0][0] = g[1][0] = 1;
for (int i = 1, t = 1, it = 1; i < mod << 1; ++i, t = (LL)t * w % mod, it = (LL)it * iw % mod)
g[0][i] = (LL)g[0][i - 1] * t % mod, g[1][i] = (LL)g[1][i - 1] * it % mod;
}
namespace Poly
{
const LL mod = 180143985094819841;
int rev[N];
LL binpow (LL b, LL k = mod - 2)
{
LL res = 1;
for (; k; k >>= 1, b = (L)b * b % mod)
if (k & 1) res = (L)res * b % mod;
return res;
}
void ntt (LL *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 1; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1) > i)
swap(x[rev[i]], x[i]);
for (int mid = 1; mid < tot; mid <<= 1)
{
LL w1 = binpow(6, (mod - 1) / (mid << 1));
if (!~op) w1 = binpow(w1);
for (int i = 0; i < tot; i += mid << 1)
{
LL k = 1;
for (int j = 0; j < mid; ++j, k = (L)k * w1 % mod)
{
LL p = x[i | j], q = (L)k * x[i | j | mid] % mod;
x[i | j] = (p + q) % mod, x[i | j | mid] = (p - q) % mod;
}
}
}
if (~op) return;
LL itot = binpow(tot);
for (int i = 0; i < tot; ++i)
x[i] = (L)x[i] * itot % mod;
}
void PolyMul (int n, LL *f, int m, LL *g, int nm, LL *res)
{
int bit = 0;
while (1 << bit < max(n, m) - 1) ++bit;
int tot = 1 << bit;
for (int i = n; i < tot; ++i) f[i] = 0;
for (int i = m; i < tot; ++i) g[i] = 0;
ntt(f, bit, 1), ntt(g, bit, 1);
for (int i = 0; i < tot; ++i)
res[i] = (L)f[i] * g[i] % mod;
ntt(res, bit, -1);
for (int i = 0; i < nm; ++i)
if (res[i] < 0) res[i] += mod;
for (int i = nm; i < tot; ++i) res[i] = 0;
}
}
void BlueStein (int *x, int n, int op)
{
static LL A[N], B[N];
for (int i = 0; i < n; ++i)
A[i] = (LL)x[i] * g[op ^ 1][i] % mod;
for (int i = 0; i < n + n - 1; ++i)
B[n + n - 2 - i] = g[op][i];
Poly::PolyMul(n, A, n + n - 1, B, n + n - 1, A);
for (int i = 0; i < n; ++i)
x[i] = A[n + n - 2 - i] % mod * g[op ^ 1][i] % mod;
if (!op) return;
int itot = binpow(n);
for (int i = 0; i < n; ++i)
x[i] = (LL)x[i] * itot % mod;
}
int main ()
{
static int n, c, A[N], B[N];
read(n, c); mod = n + 1;
init();
for (int i = 0; i < n; ++i) read(A[i]);
for (int i = 0; i < n; ++i) read(B[i]);
BlueStein(A, n, 0), BlueStein(B, n, 0);
for (int i = 0; i < n; ++i)
A[i] = (LL)A[i] * binpow(B[i], c) % mod;
BlueStein(A, n, 1);
for (int i = 0; i < n; ++i)
write((A[i] + mod) % mod), puts("");
return 0;
}