結果

問題 No.2428 Returning Shuffle
ユーザー lam6er
提出日時 2025-03-31 17:37:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 1,447 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,916 KB
実行使用メモリ 297,948 KB
最終ジャッジ日時 2025-03-31 17:37:48
合計ジャッジ時間 8,006 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from math import gcd

def main():
    MOD = 998244353
    input = sys.stdin.read().split()
    ptr = 0
    N, M = int(input[ptr]), int(input[ptr + 1])
    ptr += 2
    Q = list(range(N + 1))  # Q[0] is unused

    for _ in range(M):
        T = int(input[ptr])
        s_list = list(map(int, input[ptr + 1:ptr + 1 + T]))
        ptr += T + 1
        # Create the predecessor mapping for this cycle
        prev = {}
        prev[s_list[0]] = s_list[-1]
        for i in range(1, T):
            prev[s_list[i]] = s_list[i - 1]
        # Compute new values for each element in the cycle
        new_vals = {}
        for y in s_list:
            new_vals[y] = Q[prev[y]]
        # Update Q with new values
        for y in s_list:
            Q[y] = new_vals[y]
    
    # Build the forward permutation P from Q
    P = [0] * (N + 1)
    for y in range(1, N + 1):
        x = Q[y]
        P[x] = y
    
    # Compute cycle decomposition and LCM of cycle lengths
    visited = [False] * (N + 1)
    current_lcm = 1
    for x in range(1, N + 1):
        if not visited[x]:
            cycle_len = 0
            y = x
            while not visited[y]:
                visited[y] = True
                y = P[y]
                cycle_len += 1
            # Update LCM
            g = gcd(current_lcm, cycle_len)
            current_lcm = (current_lcm * cycle_len) // g
    
    print(current_lcm % MOD)

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