A Sequence of Permutations
A Sequence of Permutations
题意
- 对于
1 \sim n 的排列p,q ,定义排列f(p,q) ,满足f(p,q) 的第p_i 个元素为q_i 。 - 定义排列的序列
a 满足,a_1 = p ,a_2 = q ,a_{n+2} = f(a_n,a_{n+1})(n \ge 1) 。 - 求
a_k 。 -
题解
注意到
记
于是得到规律:
快速幂即可,时间复杂度
代码
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;
}