結果

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

ソースコード

diff #

import sys
from collections import defaultdict

MOD = 10**9 + 7

def factorize(x):
    factors = defaultdict(int)
    if x == 0:
        return factors
    while x % 2 == 0:
        factors[2] += 1
        x = x // 2
    i = 3
    while i * i <= x:
        while x % i == 0:
            factors[i] += 1
            x = x // i
        i += 2
    if x > 1:
        factors[x] += 1
    return factors

def get_divisors_sorted(x):
    if x == 0:
        return []
    factors = factorize(x)
    divisors = [1]
    for prime, exp in factors.items():
        current_divisors = []
        prime_powers = [prime**e for e in range(1, exp + 1)]
        for d in divisors:
            for p_pow in prime_powers:
                current_divisors.append(d * p_pow)
        divisors += current_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_prime(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 in (1, 4):
        x = p - 1
    else:
        x = 2 * (p + 1)
    divisors = get_divisors_sorted(x)
    for d in divisors:
        a, b = fast_doubling(d, p)
        if a == 0 and b == 1:
            return d
    return x

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    max_factors = defaultdict(int)
    for _ in range(N):
        p = int(input[idx])
        k = int(input[idx + 1])
        idx += 2
        if p == 2:
            factors = {2: k - 1, 3: 1}
        elif p == 5:
            factors = {2: 2, 5: k}
        else:
            d = compute_pisano_prime(p)
            factors_d = factorize(d)
            factors_d[p] += (k - 1)
            factors = factors_d
        for prime, exp in factors.items():
            if exp > max_factors[prime]:
                max_factors[prime] = exp
    result = 1
    for prime, exp in max_factors.items():
        result = (result * pow(prime, exp, MOD)) % MOD
    print(result)

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