結果
| 問題 | No.3529 2p Teleportations |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-15 10:52:33 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 209 ms / 2,000 ms |
| コード長 | 1,318 bytes |
| 記録 | |
| コンパイル時間 | 345 ms |
| コンパイル使用メモリ | 85,764 KB |
| 実行使用メモリ | 88,992 KB |
| 最終ジャッジ日時 | 2026-05-04 20:54:29 |
| 合計ジャッジ時間 | 20,735 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 49 |
ソースコード
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(p,L);
while len(cycles) > 1:
u = cycles.pop()
v = cycles.pop()
for k in range(L):
B[u[k]] = v[k]
B[v[k]] = u[(k-d)%L]
if cycles:
d = modinv(2*p,L-1);
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()