結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー lam6er
提出日時 2025-03-20 21:00:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 48 ms / 5,000 ms
コード長 1,073 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 61,944 KB
最終ジャッジ日時 2025-03-20 21:00:40
合計ジャッジ時間 3,283 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

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()
0