結果

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

ソースコード

diff #

import sys
from itertools import product
from collections import defaultdict

MOD = 10**9 + 7

def legendre_symbol(a, p):
    ls = pow(a, (p - 1) // 2, p)
    if ls == p - 1:
        return -1
    return ls

def factorize(x):
    factors = defaultdict(int)
    if x % 2 == 0:
        cnt = 0
        while x % 2 == 0:
            cnt += 1
            x = x // 2
        factors[2] += cnt
    i = 3
    while i * i <= x:
        if x % i == 0:
            cnt = 0
            while x % i == 0:
                cnt += 1
                x = x // i
            factors[i] += cnt
        i += 2
    if x > 1:
        factors[x] += 1
    return factors

def fast_doubling(n, mod):
    if n == 0:
        return (0, 1)
    a, b = fast_doubling(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 main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    
    max_factors = defaultdict(int)
    
    for _ in range(N):
        p = int(input[ptr])
        k = int(input[ptr+1])
        ptr +=2
        
        if p == 2:
            current_factors = defaultdict(int)
            current_factors[2] = k - 1
            current_factors[3] = 1
        elif p == 5:
            current_factors = defaultdict(int)
            current_factors[2] = 2
            current_factors[5] = k
        else:
            c = legendre_symbol(5, p)
            if c == 1:
                m = p - 1
            else:
                m = 2 * (p + 1)
            
            m_factors = factorize(m)
            
            primes = list(m_factors.keys())
            exponents = [list(range(e + 1)) for e in m_factors.values()]
            
            divisors = set()
            for exps in product(*exponents):
                divisor = 1
                for i in range(len(primes)):
                    divisor *= primes[i] ** exps[i]
                divisors.add(divisor)
            divisors = sorted(divisors)
            
            d_pisano = None
            for d in divisors:
                if d == 0:
                    continue
                f_d, f_d_plus_1 = fast_doubling(d, p)
                if f_d % p == 0 and f_d_plus_1 % p == 1:
                    d_pisano = d
                    break
            
            d_factors = factorize(d_pisano)
            current_factors = defaultdict(int)
            for q, e in d_factors.items():
                current_factors[q] += e
            current_factors[p] += k - 1
        
        for q in current_factors:
            e = current_factors[q]
            if max_factors[q] < e:
                max_factors[q] = e
    
    result = 1
    for q in max_factors:
        e = max_factors[q]
        result = (result * pow(q, e, MOD)) % MOD
    
    print(result)

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