結果

問題 No.3529 2p Teleportations
コンテスト
ユーザー titia
提出日時 2026-06-23 18:29:41
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
RE  
実行時間 -
コード長 4,288 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,938 ms
コンパイル使用メモリ 84,852 KB
実行使用メモリ 97,176 KB
最終ジャッジ日時 2026-06-23 18:30:00
合計ジャッジ時間 18,836 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9 RE * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

T=int(input())

for tests in range(T):
    N,P=list(map(int,input().split()))
    A=list(map(int,input().split()))
    
    
    for i in range(N):
        A[i]-=1

    A_INV=[-1]*N
    for i in range(N):
        A_INV[A[i]]=i
        
    USE=[0]*N

    ANS=[-1]*N
    LIST=[[] for i in range(N+1)]

    for i in range(N):
        if USE[i]==0:
            now=i
            L=[now]

            while True:
                USE[now]=1
                now=A[now]

                if now==L[0]:
                    break
                L.append(now)
            LIST[len(L)].append(L)

    #print(LIST)

    for i in range(1,N+1):
        if LIST[i]==[]:
            continue
        #print(i,ANS)
        if i==1:
            for L in LIST[i]:
                ANS[L[0]]=L[0]
        elif i%2==0:
            for j in range(0,len(LIST[i]),2):
                if j+1>=len(LIST[i]):
                    break

                L=LIST[i][j]+LIST[i][j+1]

                k=2*P%len(L)

                if k==0:
                    for x in L:
                        ANS[x]=x
                    continue

                X=[-1]*len(L)
                X[0]=A[L[0]]
                X[k]=L[0]
                ind=k

                while True:
                    now=X[ind]
                    c=A_INV[now]
                    if X[(ind+k)%len(L)]==-1:
                        X[(ind+k)%len(L)]=c
                        ind=(ind+k)%len(L)
                    else:
                        break

                X[1]=A[L[i]]
                X[1+k]=L[i]
                ind=1+k

                while True:
                    now=X[ind]
                    c=A_INV[now]
                    if X[(ind+k)%len(L)]==-1:
                        X[(ind+k)%len(L)]=c
                        ind=(ind+k)%len(L)
                    else:
                        break

                for ix in range(len(X)):
                    ANS[X[ix]]=X[(ix+1)%len(X)]

            if len(LIST[i])%2==1:
                L=LIST[i][-1]
                #print(L)

                #cx=L.pop()
                k=2*P%(len(L)-1)
                #print(L)

                if k==0:
                    for x in L:
                        ANS[x]=L[0]
                    continue

                X=[-1]*(len(L)-1)
                X[0]=A[L[0]]
                X[k]=L[0]
                ind=k

                #print("!!!",L)

                while True:
                    now=X[ind]
                    c=A_INV[now]
                    if X[(ind+k)%(len(L)-1)]==-1:
                        X[(ind+k)%(len(L)-1)]=c
                        ind=(ind+k)%(len(L)-1)
                    else:
                        break

                #print("!",X)

                for i in range(len(X)):
                    ANS[X[i]]=X[(i+1)%len(X)]

                SET=set(L)
                for x in X:
                    SET.remove(x)

                rest=SET.pop()

                #print(ANS,rest)
                kk=A_INV[rest]
                #print(kk)
                ind=X.index(kk)
                u=X[ind-k+1]

                ANS[rest]=u

        else:
            for j in range(0,len(LIST[i])):
                L=LIST[i][j]
                k=2*P%len(L)

                if k==0:
                    for x in L:
                        ANS[x]=x
                    continue

                X=[-1]*len(L)
                X[0]=A[L[0]]
                X[k]=L[0]

                ind=k

                while True:
                    now=X[ind]
                    c=A_INV[now]
                    if X[(ind+k)%len(L)]==-1:
                        X[(ind+k)%len(L)]=c
                        ind=(ind+k)%len(L)
                    else:
                        break

                for i in range(len(X)):
                    ANS[X[i]]=X[(i+1)%len(X)]


    #NOW=A[:]
    #print("?",ANS)

    #for tt in range(P*2):
    #    TO=[-1]*N
    #    for i in range(N):
    #        TO[i]=ANS[NOW[i]]
    #    NOW=TO

    #wrong=0
    #for i in range(N):
    #    if NOW[i]!=i:
    #        wrong+=1

    #print(NOW)

    #assert wrong*wrong<N
 

    for i in range(N):
        ANS[i]+=1
        ANS[i]=max(ANS[i],1)

    print(*ANS)
           

            

            

    

    
0