AtCoder AGC031D A Sequence of Permutations (group theory, rapid replacement power)

Topic Link

https://atcoder.jp/contests/agc031/tasks/agc031_d

answer

This is actually really a problem to find the law of God. . .

Some basic definitions first understand substitutions, substitutions \ (P \) and \ (Q \) composite \ (A \) is defined as \ (a_i = P_ {Q_I} \) , denoted \ (a = pq \) there Theorem \ ((PQ) ^ {-. 1} = Q ^ {-. 1} P ^ {-. 1} \) .
obviously the title defined in \ (f (p, q) = qp ^ {- 1} \ ) .
then hit the first few hit table:
\ (A_1 = P \)
\ (A_2 = Q \)
\ (QP A_3 = ^ {-}. 1 \)
\ (^ {A_4 QP = -. 1 Q ^ {} - . 1} \)
\ (A_5 = QP ^ {-. 1} Q ^ {-. 1} PQ ^ {-. 1} \)
\ (a_6 = QP ^ {-. 1} Q ^ {-. 1} P ^ 2q ^ {- . 1} \)
\ (A_7 = QP ^ {-. 1} Q ^ {-. 1} pqpq ^ {-. 1} \)
\ (a_8 = QP ^ {-. 1} Q ^ {-. 1} PQP ^ {-. 1} qpq ^ {- 1} \)
if the law is not obvious ah ...... ......
well, concluded that \ (A_N Ga_ = {G}. 6-n-^ {-}. 1 \) , where \ (g = qp ^ { } q ^ {-1 - the p-1} \) (which is how to look out for ......).
know conclusion, we proved relatively easy to generalize: obviously\ (GG ^ {-. 1} = E \) ( \ (E \) is the unit element, \ (e_i = I \) ), then \ (a_n = (ga_ {n -7} g ^ {- 1}) (ga_ {n-8} g ^ {- 1}) ^ (- 1) = ga_ {n-7} a_ {n-8} ^ {- 1} g ^ {- 1} = ga_ {n-6} G ^ {-. 1} \) .
so disposed \ (m '= \ lfloor \ FRAC {m-. 1} {. 6} \ rfloor, n-' = m-6M '\) , \ (A_N = G ^ {m' n'g ^ {} - m '} \) , direct and rapid computing power to, the time complexity \ (O (n \ log m ) \) or \ (O (n-) \) .

Code

#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<iostream>
using namespace std;

inline int read()
{
    int x=0; bool f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
    if(f) return x;
    return -x;
}

const int N = 1e5;
const int lgM = 30;
int p[N+3],q[N+3],pp[N+3],qq[N+3];
int g[N+3],f[N+3],ff[N+3];
int tmp[N+3];
int aux[N+3];
int ans[N+3];
int a[7][N+3];
int n,m;

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++) scanf("%d",&p[i]),pp[p[i]] = i;
    for(int i=1; i<=n; i++) scanf("%d",&q[i]),qq[q[i]] = i;
    for(int i=1; i<=n; i++) a[1][i] = p[i],a[2][i] = q[i];
    for(int k=3; k<=6; k++)
    {
        for(int i=1; i<=n; i++) a[k][a[k-2][i]] = a[k-1][i];
    }
    for(int i=1; i<=n; i++) g[i] = q[pp[qq[p[i]]]],f[i] = i,tmp[i] = g[i];
    int nn = m%6==0?6:m%6; m = (m-1)/6;
    for(int i=0; m; i++)
    {
        if(m&(1<<i))
        {
            m-=(1<<i);
            for(int j=1; j<=n; j++) aux[j] = f[tmp[j]];
            for(int j=1; j<=n; j++) f[j] = aux[j];
        }
        for(int j=1; j<=n; j++) aux[j] = tmp[tmp[j]];
        for(int j=1; j<=n; j++) tmp[j] = aux[j];
    }
    for(int i=1; i<=n; i++) ff[f[i]] = i;
    for(int i=1; i<=n; i++) ans[i] = f[a[nn][ff[i]]];
    for(int i=1; i<=n; i++) printf("%d ",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/11517907.html