結果

問題 No.2428 Returning Shuffle
ユーザー Theta
提出日時 2024-03-06 19:22:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,685 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 377,100 KB
最終ジャッジ日時 2024-09-29 18:33:37
合計ジャッジ時間 13,412 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import chain
from math import lcm


def main():
    N, M = map(int, input().split())
    replaces = []
    for _ in range(M):
        _, *S = map(lambda n: int(n) - 1, input().split())
        replaces.append(list(S))

    array = list(range(N))

    for replace_ in replaces:
        prev = -1
        for pos, new in zip(replace_, chain(replace_[-1:], replace_[:-1])):

            if prev == -1:
                array[pos], prev = array[new], array[pos]
            else:
                array[pos], prev = prev, array[pos]
    corresponds = dict(enumerate(array))

    rest = set(range(N))
    interval = [1] * N
    for idx in range(N):
        if idx not in rest:
            continue
        current = idx
        current = corresponds[current]
        while current != idx:
            interval[idx] += 1
            rest.discard(current)
            current = corresponds[current]
        rest.discard(idx)
    print(lcm(*interval) % 998244353)


if __name__ == "__main__":
    main()
0