結果
| 問題 | No.3038 シャッフルの再現 |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-16 00:15:48 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,396 bytes |
| 記録 | |
| コンパイル時間 | 166 ms |
| コンパイル使用メモリ | 81,824 KB |
| 実行使用メモリ | 67,236 KB |
| 最終ジャッジ日時 | 2025-04-16 00:17:35 |
| 合計ジャッジ時間 | 2,028 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 21 |
ソースコード
import sys
from math import gcd
from functools import reduce
MOD = 10**9 + 7
def factor(n):
factors = {}
while n % 2 == 0:
factors[2] = factors.get(2, 0) + 1
n = n // 2
i = 3
while i * i <= n:
while n % i == 0:
factors[i] = factors.get(i, 0) + 1
n = n // i
i += 2
if n > 1:
factors[n] = 1
return factors
def generate_divisors(factors):
divisors = [1]
for p, exp in factors.items():
current_divisors = []
for d in divisors:
current = 1
for _ in range(exp + 1):
current_divisors.append(d * current)
current *= p
divisors = current_divisors
divisors = list(set(divisors))
divisors.sort()
return divisors
def fib_pair(n, mod):
if mod == 1:
return (0, 0)
def fib(n_val):
if n_val == 0:
return (0, 1)
a, b = fib(n_val >> 1)
c = (a * (2 * b - a)) % mod
d = (a * a + b * b) % mod
if n_val & 1:
return (d, (c + d) % mod)
else:
return (c, d)
return fib(n)
def compute_pisano_period(p):
if p == 2:
return 3
if p == 5:
return 20
legendre = pow(5, (p - 1) // 2, p)
if legendre == 1:
m = p - 1
else:
m = 2 * (p + 1)
factors = factor(m)
divisors = generate_divisors(factors)
for d in divisors:
if d == 0:
continue
fib_d, fib_d_plus_1 = fib_pair(d, p)
if fib_d % p == 0 and fib_d_plus_1 % p == 1:
return d
return m
def compute_period_prime_power(p, k):
if p == 2:
if k == 1:
return 3
elif k == 2:
return 6
else:
return 3 * (2 ** (k - 1))
elif p == 5:
return 20 * (5 ** (k - 1))
else:
pisano_p = compute_pisano_period(p)
return pisano_p * (p ** (k - 1))
def main():
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx += 1
periods = []
for _ in range(N):
p = int(input[idx])
k = int(input[idx + 1])
idx += 2
period = compute_period_prime_power(p, k)
periods.append(period)
def lcm(a, b):
return a * b // gcd(a, b)
result = reduce(lcm, periods, 1)
print(result % MOD)
if __name__ == '__main__':
main()
lam6er