結果

問題 No.3529 2p Teleportations
コンテスト
ユーザー detteiuu
提出日時 2026-08-22 03:17:47
言語 PyPy3
(7.3.23)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,391 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 480 ms
コンパイル使用メモリ 95,856 KB
実行使用メモリ 99,756 KB
最終ジャッジ日時 2026-08-22 03:18:17
合計ジャッジ時間 24,202 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3 WA * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from sys import stdin
input = stdin.readline

def func(A, cnt):
    ans = [-1]*len(A)
    idx = 0
    for a in A:
        while ans[idx] != -1:
            idx += 1
            idx %= len(A)
        ans[idx] = a
        idx += cnt
        idx %= len(A)
    return ans

for _ in range(int(input())):
    N, P = map(int, input().split())
    A = list(map(int, input().split()))

    for i in range(N): A[i] -= 1

    ans = [-1]*N
    F = [False]*N
    G = [[] for _ in range(N+1)]
    for i in range(N):
        if F[i]: continue
        route = [i]
        F[i] = True
        idx = A[i]
        while idx != i:
            route.append(idx)
            F[idx] = True
            idx = A[idx]

        if len(route) == 1:
            ans[i] = i
            continue
        G[len(route)].append(tuple(route))

    for i in range(N+1):
        for j in range(len(G[i])//2):
            l, r = G[i][j*2], G[i][j*2+1]
            L, R = func(l, P%i), func(r, P%i)
            for k in range(i):
                ans[L[k]] = R[k]
                ans[R[k]] = L[(k+1)%i]
        if len(G[i])%2 == 1:
            if i == 2:
                l, r = G[i][-1]
                ans[l] = l
                ans[r] = l
            else:
                route = func(G[i][-1], P*2%i)
                for k in range(i):
                    ans[route[k]] = route[(k+1)%len(route)]

    print(*[a+1 for a in ans])
0