結果

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

ソースコード

diff #

import sys
import random
from math import gcd

MOD = 10**9 + 7

def is_prime(n):
    if n < 2:
        return False
    for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
        if n % p == 0:
            return n == p
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
        if a >= n:
            continue
        x = pow(a, d, n)
        if x == 1 or x == n - 1:
            continue
        for _ in range(s - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True

def pollards_rho(n):
    if n % 2 == 0:
        return 2
    if n % 3 == 0:
        return 3
    while True:
        c = random.randint(1, n - 1)
        f = lambda x: (pow(x, 2, n) + c) % n
        x, y, d = 2, 2, 1
        while d == 1:
            x = f(x)
            y = f(f(y))
            d = gcd(abs(x - y), n)
        if d != n:
            return d

def factor(n):
    factors = {}
    def _factor(n):
        if n == 1:
            return
        if is_prime(n):
            factors[n] = factors.get(n, 0) + 1
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    return factors

def generate_factors(factors):
    factors_list = []
    def dfs(index, current):
        if index == len(factors):
            factors_list.append(current)
            return
        p, exp = factors[index]
        for e in range(0, exp + 1):
            dfs(index + 1, current * (p ** e))
    dfs(0, 1)
    factors_list.sort()
    return factors_list

def compute_pisano_period(p):
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 in [1, 4]:
        m = p - 1
    else:
        m = 2 * (p + 1)
    if m == 0:
        return 1
    factors = factor(m)
    factors_list = list(factors.items())
    factors = generate_factors(factors_list)
    for d in factors:
        if d == 0:
            continue
        a, b = fib(d, p)
        if a == 0 and b == 1:
            return d
    return m

def fib(n, mod):
    if n == 0:
        return (0, 1)
    a, b = fib(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 lcm(a, b):
    return a * b // gcd(a, b)

def main():
    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])
        ptr += 2
        primes.append((p, k))
    pisano_periods = []
    for p, k in primes:
        if p == 5:
            period = 20 * (5 ** (k - 1))
        else:
            period_p = compute_pisano_period(p)
            period = period_p * (p ** (k - 1))
        pisano_periods.append(period)
    current_lcm = 1
    for p in pisano_periods:
        current_lcm = lcm(current_lcm, p)
    print(current_lcm % MOD)

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