結果

問題 No.3038 シャッフルの再現
ユーザー lam6er
提出日時 2025-03-26 15:49:03
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,909 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 68,864 KB
最終ジャッジ日時 2025-03-26 15:50:07
合計ジャッジ時間 2,895 ms
ジャッジサーバーID
(参考情報)
judge3 / 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, 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
    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 = {}
    small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for p in small_primes:
        if n % p == 0:
            cnt = 0
            while n % p == 0:
                cnt += 1
                n = n // p
            factors[p] = cnt
        if n == 1:
            return factors
    if is_prime(n):
        factors[n] = 1
        return factors
    def _factor(n):
        if n == 1:
            return {}
        if is_prime(n):
            return {n: 1}
        d = pollards_rho(n)
        a = _factor(d)
        b = _factor(n // d)
        res = {}
        for p in a:
            res[p] = a[p]
        for p in b:
            if p in res:
                res[p] += b[p]
            else:
                res[p] = b[p]
        return res
    remaining_factors = _factor(n)
    for p in remaining_factors:
        factors[p] = factors.get(p, 0) + remaining_factors[p]
    return factors

def generate_divisors(factors_dict):
    divisors = [1]
    for p in sorted(factors_dict.keys()):
        exp = factors_dict[p]
        current_powers = [p**e for e in range(exp+1)]
        new_divisors = []
        for d in divisors:
            for power in current_powers:
                new_divisors.append(d * power)
        divisors = list(set(new_divisors))
    divisors = sorted(divisors)
    return divisors

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

def compute_pisano_period(p):
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 in (1, 4):
        base = p - 1
    else:
        base = 2 * (p + 1)
    factors = factor(base)
    divisors = generate_divisors(factors)
    for d in sorted(divisors):
        if d == 0:
            continue
        fn, fn_plus_1 = fib_pair(d, p)
        if fn == 0 and fn_plus_1 == 1 % p:
            return d
    return base

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
        pisano_p = compute_pisano_period(p)
        factors_pisano = factor(pisano_p)
        if p in factors_pisano:
            factors_pisano[p] += (k - 1)
        else:
            factors_pisano[p] = (k - 1)
        for p_factor, exp in factors_pisano.items():
            if p_factor in lcm_factors:
                if exp > lcm_factors[p_factor]:
                    lcm_factors[p_factor] = exp
            else:
                lcm_factors[p_factor] = exp
    result = 1
    for p, exp in lcm_factors.items():
        result = (result * pow(p, exp, MOD)) % MOD
    print(result)

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