結果

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

ソースコード

diff #

import sys
MOD = 10**9 + 7

def fast_doubling(n, p):
    if n == 0:
        return (0, 1)
    a, b = fast_doubling(n >> 1, 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 check_pisano(d, p):
    if d == 0:
        return False
    a, b = fast_doubling(d, p)
    return (a % p == 0) and (b % p == 1)

def factor(n):
    factors = {}
    while n % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        n //= 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] = factors.get(i, 0) + 1
            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 = []
        for d in divisors:
            current = 1
            for _ in range(exp + 1):
                temp.append(d * current)
                current *= p
        divisors = temp
    return divisors

def compute_period_pisano(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    legendre = pow(5, (p - 1) // 2, p)
    if legendre == 1 or legendre == 0:
        target = p - 1
    else:
        target = 2 * (p + 1)
    factors = factor(target)
    divisors = generate_divisors(factors)
    divisors = sorted(divisors)
    for d in divisors:
        if d == 0:
            continue
        if check_pisano(d, p):
            return d
    return target

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

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    current_lcm = 1
    for _ in range(N):
        p = int(input[idx])
        k = int(input[idx + 1])
        idx += 2
        if p == 2:
            pe_pisano = 3 * (2 ** (k - 1))
        elif p == 5:
            pe_pisano = 20 * (5 ** (k - 1))
        else:
            pi_p = compute_period_pisano(p)
            pe_pisano = pi_p * (p ** (k - 1))
        current_lcm = lcm(current_lcm, pe_pisano)
    print(current_lcm % MOD)

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