結果

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

ソースコード

diff #

import sys
from math import gcd
from functools import reduce

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

def get_prime_factors(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 > 2:
        factors[n] = 1
    return factors

def generate_divisors(factors):
    divisors = [1]
    for prime, exp in factors.items():
        temp = []
        prime_pows = [prime**e for e in range(1, exp + 1)]
        for d in divisors:
            for pow_ in prime_pows:
                temp.append(d * pow_)
        divisors += temp
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

def calculate_pisano_prime(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    mod = p % 5
    if mod in (1, 4):
        m = p - 1
    else:
        m = 2 * (p + 1)
    factors = get_prime_factors(m)
    divisors = generate_divisors(factors)
    for d in divisors:
        if d == 0:
            continue
        f_d_plus_1, f_d_plus_2 = fib_mod(d, p)
        f_d = f_d_plus_1
        if f_d % p == 0 and f_d_plus_2 % p == 1:
            return d
    return m  # Fallback, though should not reach here for valid primes

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

def main():
    MOD = 10**9 + 7
    input = sys.stdin.read().split()
    ptr = 0
    n = int(input[ptr])
    ptr += 1
    primes = []
    for _ in range(n):
        p = int(input[ptr])
        k = int(input[ptr + 1])
        primes.append((p, k))
        ptr += 2
    pisano_periods = []
    for p, k in primes:
        if p == 2:
            if k == 1:
                pi = 3
            else:
                pi = 3 * (2 ** (k - 1))
        elif p == 5:
            if k == 1:
                pi = 20
            else:
                pi = 20 * (5 ** (k - 1))
        else:
            pi_p = calculate_pisano_prime(p)
            pi = pi_p * (p ** (k - 1))
        pisano_periods.append(pi)
    if not pisano_periods:
        print(0)
        return
    total_lcm = reduce(lcm, pisano_periods)
    print(total_lcm % MOD)

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