結果

問題 No.3038 シャッフルの再現
ユーザー gew1fw
提出日時 2025-06-12 13:46:54
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,207 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 67,612 KB
最終ジャッジ日時 2025-06-12 13:47:04
合計ジャッジ時間 2,706 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 1
other RE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

MOD = 10**9 + 7

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

def generate_divisors(factors):
    divisors = [1]
    for p in factors:
        exponents = []
        current = 1
        for _ in range(factors[p]):
            current *= p
            exponents.append(current)
        new_divisors = []
        for d in divisors:
            for exp in exponents:
                new_divisors.append(d * exp)
        divisors += new_divisors
    divisors = list(sorted(set(divisors)))
    return divisors

def fib(n, mod):
    if n == 0:
        return (0, 1)
    a, b = fib(n >> 1, mod)
    c = (a * ((2 * b - a) % mod)) % 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):
        n = p - 1
    else:
        n = 2 * (p + 1)
    factors = factorize(n)
    divisors = generate_divisors(factors)
    for d in divisors:
        f_d, f_d_plus_1 = fib(d, p)
        if f_d == 0 and f_d_plus_1 == 1:
            return d
    return n

def compute_period(p, k):
    if p == 2:
        return 3 * (2 ** (k - 1))
    elif p == 5:
        return 20 * (5 ** (k - 1))
    else:
        base = compute_pisano_period(p)
        return base * (p ** (k - 1))

def lcm(a, b):
    return a * b // math.gcd(a, b)

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    periods = []
    for _ in range(N):
        p = int(input[idx])
        k = int(input[idx + 1])
        idx += 2
        period = compute_period(p, k)
        periods.append(period)
    current_lcm = 1
    for p in periods:
        current_lcm = lcm(current_lcm, p)
    print(current_lcm % MOD)

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