結果
| 問題 | No.101 ぐるぐる!あみだくじ! |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 18:45:46 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 46 ms / 5,000 ms |
| コード長 | 1,073 bytes |
| コンパイル時間 | 317 ms |
| コンパイル使用メモリ | 82,688 KB |
| 実行使用メモリ | 61,776 KB |
| 最終ジャッジ日時 | 2025-03-20 18:46:01 |
| 合計ジャッジ時間 | 3,234 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
import sys
from math import gcd
def main():
N = int(sys.stdin.readline())
K = int(sys.stdin.readline())
bars = []
for _ in range(K):
X, Y = map(int, sys.stdin.readline().split())
bars.append((X, Y))
# Calculate permutation after one shuffle
perm = list(range(N + 1)) # 0-based index for ease, perm[1..N] are used
for i in range(1, N + 1):
current = i
for X, Y in bars:
if current == X:
current = Y
elif current == Y:
current = X
perm[i] = current
# Find cycles and compute LCM of cycle lengths
visited = [False] * (N + 1)
lcm = 1
for i in range(1, N + 1):
if not visited[i]:
cycle_length = 0
j = i
while not visited[j]:
visited[j] = True
j = perm[j]
cycle_length += 1
current_gcd = gcd(lcm, cycle_length)
lcm = (lcm * cycle_length) // current_gcd
print(lcm)
if __name__ == "__main__":
main()
lam6er