結果

問題 No.3038 シャッフルの再現
ユーザー lam6er
提出日時 2025-04-15 23:30:08
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,493 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,124 KB
実行使用メモリ 70,236 KB
最終ジャッジ日時 2025-04-15 23:31:21
合計ジャッジ時間 2,731 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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]:
        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 = 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_divisors(factors_dict):
    divisors = [1]
    for p, exp in factors_dict.items():
        temp = []
        current_power = 1
        for _ in range(exp + 1):
            for d in divisors:
                temp.append(d * current_power)
            current_power *= p
        divisors = list(set(temp))
    divisors.sort()
    return divisors

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

def compute_pisano_prime(p):
    if p == 2:
        return {3: 1}
    if p == 5:
        return {2: 2, 5: 1}
    mod5 = p % 5
    if mod5 in (1, 4):
        m = p - 1
    else:
        m = 2 * (p + 1)
    factors_m = factor(m)
    divisors = generate_divisors(factors_m)
    per_p = m
    for d in divisors:
        a, b = fib_pair(d, p)
        if a == 0 and b == 1 % p:
            per_p = d
            break
    per_p_factors = factor(per_p)
    return per_p_factors

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    lcm_factors = {}
    for _ in range(N):
        p = int(input[ptr])
        ptr += 1
        k = int(input[ptr])
        ptr += 1
        if p == 2:
            if k == 1:
                current_factors = {3: 1}
            else:
                current_factors = {2: (k - 1), 3: 1}
        elif p == 5:
            current_factors = {2: 2, 5: k}
        else:
            per_p_factors = compute_pisano_prime(p)
            current_factors = per_p_factors.copy()
            current_power = k - 1
            if current_power > 0:
                current_factors[p] = current_factors.get(p, 0) + current_power
        for q in current_factors:
            exp = current_factors[q]
            if exp == 0:
                continue
            if q not in lcm_factors or exp > lcm_factors[q]:
                lcm_factors[q] = exp
    result = 1
    for q in lcm_factors:
        result = (result * pow(q, lcm_factors[q], MOD)) % MOD
    print(result)

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