結果

問題 No.3529 2p Teleportations
コンテスト
ユーザー GaLLium
提出日時 2026-05-04 18:15:35
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,087 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,507 ms
コンパイル使用メモリ 84,864 KB
実行使用メモリ 89,856 KB
最終ジャッジ日時 2026-05-04 20:59:21
合計ジャッジ時間 24,274 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13 WA * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#嘘解法
def modinv(x, m):
    for i in range(m):
        if (x*i-1) % m == 0:
            return i

def solve():
    N,p = map(int,input().split())
    A = [0] + list(map(int,input().split()))
    
    # サイクルを長さごとに列挙
    groups = [[] for _ in range(N+1)]
    seen = [False]*(N+1)
    for i in range(1,N+1):
        if seen[i]:
            continue
        v = []
        while not seen[i]:
            seen[i] = True;
            v.append(i)
            i = A[i];
        groups[len(v)].append(v);

    # 構築
    B = [0]*(N+1)
    for (L,cycles) in enumerate(groups): 
        if not cycles:
            continue
        if L % 2 == 1:
            d = modinv(2*p,L);
            while cycles:
                v = cycles.pop()
                for k in range(L):
                    B[v[k]] = v[(k-d)%L]
        else:
            d = modinv(2*p,L-1);
            while cycles:
                v = cycles.pop()
                for k in range(L):
                    B[v[k]] = v[(k-d)%(L-1)];
    print(*B[1:])
        
for _ in range(int(input())):
    solve()
0