結果

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

ソースコード

diff #

import sys
import math
import random
from collections import defaultdict

def is_prime(n):
    if n < 2:
        return False
    for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]:
        if n % p == 0:
            return n == p
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
        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
    if n % 5 == 0:
        return 5
    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 = math.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.append(n)
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    factors.sort()
    return factors

def multiply(a, b, mod):
    a11, a12, a21, a22 = a
    b11, b12, b21, b22 = b
    c11 = (a11 * b11 + a12 * b21) % mod
    c12 = (a11 * b12 + a12 * b22) % mod
    c21 = (a21 * b11 + a22 * b21) % mod
    c22 = (a21 * b12 + a22 * b22) % mod
    return (c11, c12, c21, c22)

def matrix_pow(matrix, power, mod):
    result = (1, 0, 0, 1)
    while power > 0:
        if power % 2 == 1:
            result = multiply(result, matrix, mod)
        matrix = multiply(matrix, matrix, mod)
        power //= 2
    return result

def fib_mod(n, mod):
    if mod == 1:
        return (0, 0)
    if n == 0:
        return (0, 1)
    matrix = (1, 1, 1, 0)
    powered = matrix_pow(matrix, n - 1, mod)
    fn = powered[0]
    fn_plus_1 = (powered[0] + powered[2]) % mod
    return (fn, fn_plus_1)

def compute_pisano_period(p):
    if p == 2:
        return 3
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 in [1, 4]:
        n = p - 1
    else:
        n = 2 * (p + 1)
    factors = factor(n)
    factor_counts = defaultdict(int)
    for prime in factors:
        factor_counts[prime] += 1
    divisors = [1]
    for prime, cnt in factor_counts.items():
        temp = []
        current_powers = [prime**i for i in range(cnt + 1)]
        for d in divisors:
            for power in current_powers:
                temp.append(d * power)
        divisors = list(set(temp))
        divisors.sort()
    divisors = sorted(divisors)
    for d in divisors:
        if d == 0:
            continue
        a, b = fib_mod(d, p)
        if a == 0 and b == 1 % p:
            return d
    return n

def main():
    MOD = 10**9 + 7
    input = sys.stdin.read().split()
    idx = 0
    n = int(input[idx])
    idx += 1
    primes = []
    for _ in range(n):
        p = int(input[idx])
        k = int(input[idx + 1])
        primes.append((p, k))
        idx += 2
    global_factors = defaultdict(int)
    for p, k in primes:
        if p == 1:
            continue
        pisano_p = compute_pisano_period(p)
        factors = factor(pisano_p)
        local_factors = defaultdict(int)
        for prime in factors:
            local_factors[prime] += 1
        if k - 1 > 0:
            local_factors[p] += (k - 1)
        for prime, cnt in local_factors.items():
            if global_factors[prime] < cnt:
                global_factors[prime] = cnt
    result = 1
    for prime, cnt in global_factors.items():
        result = (result * pow(prime, cnt, MOD)) % MOD
    print(result)

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