結果
| 問題 | No.3038 シャッフルの再現 |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-15 23:36:44 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,449 bytes |
| 記録 | |
| コンパイル時間 | 201 ms |
| コンパイル使用メモリ | 82,388 KB |
| 実行使用メモリ | 70,424 KB |
| 最終ジャッジ日時 | 2025-04-15 23:37:28 |
| 合計ジャッジ時間 | 2,297 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 21 |
ソースコード
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 = {}
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 = []
p_pows = [p**e for e in range(exp + 1)]
for d in divisors:
for pow_p in p_pows:
temp.append(d * pow_p)
divisors = temp
divisors = sorted(divisors)
return divisors
def compute_fib_pair(n, mod):
if n == 0:
return (0, 1)
x, y = 0, 1
mask = 1 << (n.bit_length() - 1)
while mask > 0:
c = (x * (2 * y - x)) % mod
d = (x * x + y * y) % mod
if (n & mask) != 0:
x, y = d, (c + d) % mod
else:
x, y = c, d
mask >>= 1
return (x, y)
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 == 5:
period_p = 20
factors_pi_period = factor(period_p)
else:
rem = p % 5
if rem in (1, 4):
base = p - 1
else:
base = 2 * (p + 1)
base_factors = factor(base)
divisors = generate_divisors(base_factors)
period_p = None
for m in divisors:
if m == 0:
continue
a, b = compute_fib_pair(m, p)
if a == 0 and b == 1:
period_p = m
break
factors_pi_period = factor(period_p)
period_pk_factors = factors_pi_period.copy()
if p in period_pk_factors:
period_pk_factors[p] += (k - 1)
else:
period_pk_factors[p] = (k - 1)
for q, e in period_pk_factors.items():
if q in lcm_factors:
if e > lcm_factors[q]:
lcm_factors[q] = e
else:
lcm_factors[q] = e
result = 1
for q, e in lcm_factors.items():
result = (result * pow(q, e, MOD)) % MOD
print(result)
if __name__ == '__main__':
main()
lam6er