結果

問題 No.3038 シャッフルの再現
ユーザー lam6er
提出日時 2025-04-16 00:10:35
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,155 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 81,772 KB
実行使用メモリ 66,964 KB
最終ジャッジ日時 2025-04-16 00:11:49
合計ジャッジ時間 2,107 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 1
other RE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

MOD = 10**9 + 7

def factor(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():
        temp = []
        current_powers = [p**e for e in range(1, exp + 1)]
        for d in divisors:
            for power in current_powers:
                temp.append(d * power)
        divisors += temp
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

def fast_doubling(n, mod):
    a, b = 0, 1
    for bit in bin(n)[2:]:
        c = a * (2 * b - a) % mod
        d = (a * a + b * b) % mod
        if bit == '1':
            a, b = d, (c + d) % mod
        else:
            a, b = c, d
    return a % mod, b % mod

def compute_pisano_prime(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    legendre = pow(5, (p - 1) // 2, p)
    if legendre == 1:
        m = p - 1
    else:
        m = 2 * (p + 1)
    factors = factor(m)
    divisors = generate_divisors(factors)
    for d in divisors:
        f_d, f_next = fast_doubling(d, p)
        if f_d == 0 and f_next == 1 % p:
            return d
    return m

def compute_pisano_prime_power(p, k):
    if p == 2:
        if k == 1:
            return 3
        else:
            return 3 * (2 ** (k - 1))
    elif p == 5:
        return 20 * (5 ** (k - 1))
    else:
        period_p = compute_pisano_prime(p)
        return period_p * (p ** (k - 1))

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_pisano_prime_power(p, k)
        periods.append(period)
    lcm = 1
    for num in periods:
        gcd = math.gcd(lcm, num)
        lcm = (lcm // gcd) * num
    print(lcm % MOD)

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