結果

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

ソースコード

diff #

import sys
import random
import math
from math import gcd
from functools import reduce

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, 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.append(n)
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    factors.sort()
    return factors

def get_divisors(factors):
    factor_counts = {}
    for p in factors:
        if p in factor_counts:
            factor_counts[p] += 1
        else:
            factor_counts[p] = 1
    divisors = [1]
    for p, cnt in factor_counts.items():
        temp = []
        for d in divisors:
            current = d
            for i in range(1, cnt + 1):
                current *= p
                temp.append(current)
        divisors += temp
    divisors = list(set(divisors))
    divisors.sort()
    return divisors

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

def compute_pisano_period(p):
    if p == 5:
        return 20
    mod5 = p % 5
    if mod5 == 1 or mod5 ==4:
        D = p -1
    else:
        D = 2 * p + 2
    factors = factor(D)
    divisors = get_divisors(factors)
    for m in divisors:
        f_m = fast_doubling(m, p)
        if f_m != 0:
            continue
        f_m_plus_1 = fast_doubling(m + 1, p)
        if f_m_plus_1 == 1:
            return m
    return D

def mod_pow(base, exponent, mod):
    result = 1
    base = base % mod
    while exponent > 0:
        if exponent % 2 == 1:
            result = (result * base) % mod
        exponent = exponent // 2
        base = (base * base) % mod
    return result

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) )
    
    periods = []
    for p, k in primes:
        if p == 2:
            if k ==1:
                period = 3
            else:
                period = 3 * (2 ** (k-1))
        else:
            pi_p = compute_pisano_period(p)
            period = pi_p * (p ** (k-1))
        periods.append(period)
    
    overall_lcm = 1
    for period in periods:
        overall_lcm = lcm(overall_lcm, period)
        overall_lcm %= MOD
    
    print(overall_lcm % MOD)

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