結果

問題 No.3038 シャッフルの再現
ユーザー lam6er
提出日時 2025-04-16 16:23:25
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,978 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 67,336 KB
最終ジャッジ日時 2025-04-16 16:24:16
合計ジャッジ時間 3,030 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 1
other RE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from math import gcd

MOD = 10**9 + 7

def factorize(n):
    factors = {}
    while n % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        n = n // 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] = factors.get(i, 0) + 1
            n = n // i
        i += 2
    if n > 1:
        factors[n] = 1
    return factors

def generate_divisors(factors):
    divisors = [1]
    for p, exp in factors.items():
        current_divisors = divisors.copy()
        new_divisors = []
        for e in range(1, exp + 1):
            pe = p ** e
            for d in current_divisors:
                new_divisors.append(d * pe)
        divisors += new_divisors
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

def fast_doubling(n, mod):
    if n == 0:
        return (0, 1)
    a, b = fast_doubling(n >> 1, mod)
    c = (a * (2 * b - a)) % mod
    d = (a * a + b * b) % mod
    if n & 1:
        return (d, (c + d) % mod)
    else:
        return (c, d)

def compute_pisano_period(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 in (1, 4):
        m = p - 1
    else:
        m = 2 * (p + 1)
    factors = factorize(m)
    divisors = generate_divisors(factors)
    for d in divisors:
        fn, fn1 = fast_doubling(d, p)
        if fn % p == 0 and fn1 % p == 1:
            return d
    return m  # This line is theoretically unreachable

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    periods = []
    for _ in range(N):
        p = int(input[ptr])
        ptr += 1
        k = int(input[ptr])
        ptr += 1
        period_p = compute_pisano_period(p)
        period_pk = period_p * (p ** (k - 1))
        periods.append(period_pk)
    current_lcm = 1
    for p in periods:
        current_lcm = current_lcm * p // gcd(current_lcm, p)
    print(current_lcm % MOD)

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