結果

問題 No.3529 2p Teleportations
コンテスト
ユーザー titia
提出日時 2026-06-22 15:02:05
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
RE  
実行時間 -
コード長 3,640 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 413 ms
コンパイル使用メモリ 85,084 KB
実行使用メモリ 97,768 KB
最終ジャッジ日時 2026-06-22 15:03:05
合計ジャッジ時間 15,372 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 2 RE * 33 TLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

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 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
                c=-1

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

                #print(L)
                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

                #print(X)

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

                #print(i,k,L,X)

                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

                #print("!",X)

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

                #print(ANS)

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

                c=L.pop()
                k=2*P%len(L)

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

                X=[-1]*len(L)
                X[0]=A[i]
                X[k]=i
                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

                #print("?",X)

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



        else:
            c=-1

            if len(L)%2==0:
                c=L.pop()

            k=2*P%len(L)

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

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

            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)]

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

    print(*ANS)
                
            

            

            

    

    
0