A Sequence of Permutations
题意
- 对于 的排列 ,定义排列 ,满足 的第 个元素为 。
- 定义排列的序列 满足,,,。
- 求 。
- ,。
题解
注意到 ,另有 ,于是有:
记 ,则 ,。
于是得到规律:,。
快速幂即可,时间复杂度 。
代码
const int N = 1e5 + 7;
int n, k, p[N], q[N], vp[N], vq[N], t[N], vt[N], ans[N];
inline void init(int *a) {
for (int i = 1; i <= n; i++) a[i] = i;
}
inline void inv(int *a, int *b) {
for (int i = 1; i <= n; i++) b[a[i]] = i;
}
inline void mul(int *a, int *b) {
static int c[N];
for (int i = 1; i <= n; i++) c[i] = a[b[i]];
for (int i = 1; i <= n; i++) a[i] = c[i];
}
inline void ksm(int *a, int b) {
static int d[N];
init(d);
while (b) {
if (b & 1) mul(d, a);
mul(a, a), b >>= 1;
}
for (int i = 1; i <= n; i++) a[i] = d[i];
}
int main() {
rd(n, k), rda(p, n), rda(q, n), inv(p, vp), inv(q, vq);
init(t), mul(t, q), mul(t, vp), mul(t, vq), mul(t, p), inv(t, vt);
int r = k % 6 ? k % 6 : 6, w = (k - r) / 6;
ksm(t, w), ksm(vt, w);
init(ans), mul(ans, t);
switch (r) {
case 1 : mul(ans, p); break;
case 2 : mul(ans, q); break;
case 3 : mul(ans, q), mul(ans, vp); break;
case 4 : mul(ans, q), mul(ans, vp), mul(ans, vq); break;
case 5 : mul(ans, q), mul(ans, vp), mul(ans, vq), mul(ans, p), mul(ans, vq); break;
case 6 : mul(ans, q), mul(ans, vp), mul(ans, vq), mul(ans, p), mul(ans, p), mul(ans, vq); break;
}
mul(ans, vt);
printa(ans, n);
return 0;
}