Loj 3058 白兔之舞

单位根反演 + 任意长度 DFT + MTT.

考虑某个 $t$ 的答案,记作 $b_t$ ,枚举白兔实际跳了 $i$ 步,得到
$$
b_t=\sum_{i=0}^L [i\bmod k=t] \binom L i A^i_{x,y}\\
=\sum_{i=0}^L [k|(i-t)]\binom L i (A^i)_{x,y}
$$
其中 $A$ 是由读入给定的 $3\times 3$ 的转移矩阵.

考虑单位根反演,继续化简,

$$
b_t=\sum_{i=0}^L \frac 1 k\sum_{j=0}^{k-1}\omega_k^{j(i-t)} \binom L i A^i_{x,y}\\
=\frac 1 k\sum_{j=0}^{k-1} \omega_k^{-jt}\sum_{i=0}^L \omega_k^{ij} A^i_{x,y} \\
=\frac 1 k\sum_{j=0}^{k-1} \omega_k^{-jt}(\omega_k^j A + I)^L_{x,y}
$$

题目保证了 $k|(p-1)$ ,所以在模 $p$ 意义下 $\omega_k =g^{\frac{p-1} k}$ ,其中 $g$ 为 $p​$ 的原根.

记 $a_j=((\omega_k^j A + I)^L)_{x,y}$ ,这可以用矩阵快速幂在 $O(n^3k\log L)$ 的时间内将 $k$ 个 $a_j$ 全部求出.
$$
b_t=\frac 1 k\sum_{j=0}^{k-1}(\omega_k^{-j})^t a_j
$$
如果 $k$ 是 $2$ 的幂次,直接用 FFT 对 $a$ 做一次长度为 $k$ 的 IDFT 即可将 $b$ 求出.

如果不保证 $k$ 是 $2$ 的幂次,就需要用到一个叫 Bluestein’s Algorithm 的算法,它可以解决任意长度的 DFT.

由组合意义知 $\binom {j+t} 2=\binom j 2 + \binom t 2 +jt$ ,于是可以把 $jt$ 拆成仅和 $j,t,j+t$ 有关的几项.

原式可以化为
$$
b_t=\frac 1 k\sum_{j=0}^{k-1} \omega_k^{\binom t 2} \cdot \omega_k^{\binom j 2}\cdot \omega_k^{-\binom{j+t}{2}} a_j \\
b_t=\frac 1 k \omega_k^{\binom t 2}\sum_{j=0}^{k-1} \omega_k^{-\binom{j+t}{2}}(a_j\cdot \omega_k^{\binom j 2})
$$

记 $f_i=\omega_k^{-\binom i 2},g_i=a_i\cdot \omega_k^{\binom i 2}$ ,则

$$
b_t=\frac 1 k \omega_k^{\binom t 2}\sum_{j=0}^{k-1} g_j\cdot f_{j+t}
$$

后面已经差不多是一个卷积了,把 $g$ 翻转一下,就能得到
$$
b_t=\frac 1 k \omega_k^{\binom t 2}\sum_{j=0}^{k-1} g^R_{k-1-j}\cdot f_{j+t} \\
b_t=\frac 1 k \omega_k^{\binom t 2}(
g^R\otimes f)_{t+k-1}
$$
用 MTT 求出 $g^R$ 与 $f​$ 的卷积即可.

时间复杂度 $O(n^3k\log L+k\log k)$ .

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out = 0, fh = 1;
char jp = getchar();
while ((jp > '9' || jp < '0') && jp != '-')
jp = getchar();
if (jp == '-')
fh = -1, jp = getchar();
while (jp >= '0' && jp <= '9')
out = out * 10 + jp - '0', jp = getchar();
return out * fh;
}
const double PI = acos(-1.0);
const int N = (1 << 18) + 10;
int P;
int add(int a, int b)
{
return a + b >= P ? a + b - P : a + b;
}
void inc(int &a, int b)
{
a = add(a, b);
}
int mul(int a, int b)
{
return 1LL * a * b % P;
}
int fpow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
struct Matrix
{
ll v[3][3];
Matrix(){memset(v, 0, sizeof v);}
ll *operator [](int x)
{
return v[x];
}
} I, A;
Matrix operator * (Matrix a, Matrix b)
{
Matrix c;
for (int i = 0; i < 3; ++i)
for (int k = 0; k < 3; ++k) if (a[i][k])
for (int j = 0; j < 3; ++j) if (b[k][j])
c[i][j] += a[i][k] * b[k][j];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
c[i][j] %= P;
return c;
}
Matrix operator * (Matrix a, int lambda)
{
Matrix b;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
b[i][j] = a[i][j] * lambda % P;
return b;
}
Matrix operator + (Matrix a, Matrix b)
{
Matrix c;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
c[i][j] = add(a[i][j], b[i][j]);
return c;
}
Matrix Fpow(Matrix a, int b)
{
Matrix res = I;
while (b)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int getrt()
{
vector<int> pr;
int x = P - 1;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
{
pr.push_back(i);
while (x % i == 0)
x /= i;
}
for (int g = 2; ; ++g)
{
bool f = true;
for (int p : pr)
if (fpow(g, (P - 1) / p) == 1)
{
f = false;
break;
}
if (f)
return g;
}
}
struct cp
{
double r, i;
cp(double r = 0, double i = 0) : r(r), i(i) {}
cp operator + (const cp &rhs) const
{
return cp(r + rhs.r, i + rhs.i);
}
cp operator - (const cp &rhs) const
{
return cp(r - rhs.r, i - rhs.i);
}
cp operator * (const cp &rhs) const
{
return cp(r * rhs.r - i * rhs.i, r * rhs.i + i * rhs.r);
}
cp conj()
{
return cp(r, -i);
}
} og[N];
int rev[N];
void DFT(cp *a, int n)
{
for (int i = 0; i < n; ++i)
if (i < rev[i])
swap(a[i], a[rev[i]]);
for (int l = 2; l <= n; l <<= 1)
{
int m = l >> 1;
for (cp *p = a; p != a + n; p += l)
for (int i = 0; i < m; ++i)
{
cp t = og[n / m * i] * p[i + m];
p[i + m] = p[i] - t;
p[i] = p[i] + t;
}
}
}
void MTT(int *a, int *b, int *c, int n)
{
static cp s1[N], s2[N], s3[N], s4[N], s5[N], s6[N];
for (int i = 0; i < n; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1));
for (int i = 0; i < n; ++i)
og[i] = cp(cos(PI * i / n), sin(PI * i / n));
for (int i = 0; i < n; ++i)
s1[i] = cp(a[i] & 32767, a[i] >> 15);
for (int i = 0; i < n; ++i)
s2[i] = cp(b[i] & 32767, b[i] >> 15);
DFT(s1, n);
DFT(s2, n);
for (int i = 0; i < n; ++i)
{
int j = (n - i) & (n - 1);
cp da = (s1[i] + s1[j].conj()) * cp(0.5, 0);
cp db = (s1[i] - s1[j].conj()) * cp(0, -0.5);
cp dc = (s2[i] + s2[j].conj()) * cp(0.5, 0);
cp dd = (s2[i] - s2[j].conj()) * cp(0, -0.5);
s3[j] = da * dc;
s4[j] = da * dd;
s5[j] = db * dc;
s6[j] = db * dd;
}
for (int i = 0; i < n; ++i)
{
s1[i] = s3[i] + s4[i] * cp(0, 1);
s2[i] = s5[i] + s6[i] * cp(0, 1);
}
DFT(s1, n);
DFT(s2, n);
for (int i = 0; i < n; ++i)
{
ll da = (ll)(s1[i].r / n + 0.5) % P;
ll db = (ll)(s1[i].i / n + 0.5) % P;
ll dc = (ll)(s2[i].r / n + 0.5) % P;
ll dd = (ll)(s2[i].i / n + 0.5) % P;
ll val = da + ((db + dc) << 15) + (dd << 30);
c[i] = val % P;
if (c[i] < 0)
c[i] += P;
}
}
int n, k, L, st, ed, omega[N], f[N], g[N], h[N], res[N];
int binom(int x)
{
return 1LL * x * (x - 1) / 2 % k;
}
int main()
{
n = read(), k = read(), L = read(), st = read(), ed = read(), P = read();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
A[i][j] = read();
for (int i = 0; i < n; ++i)
I[i][i] = 1;
omega[0] = 1, omega[1] = fpow(getrt(), (P - 1) / k);
for (int i = 2; i <= k; ++i)
omega[i] = mul(omega[i - 1], omega[1]);
for (int i = 0; i < k; ++i)
g[k - 1 - i] = mul(Fpow(A * omega[i] + I, L)[st - 1][ed - 1], omega[binom(i)]);
for (int i = 0; i < 2 * k; ++i)
f[i] = omega[k - binom(i)];
int len = 1;
while (len < 3 * k)
len <<= 1;
MTT(f, g, h, len);
int invk = fpow(k, P - 2);
for (int i = 0; i < k; ++i)
{
int res = mul(invk, mul(omega[binom(i)], h[i + k - 1]));
printf("%d\n", res);
}
return 0;
}