結果
| 問題 |
No.3038 シャッフルの再現
|
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 20:38:25 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,316 bytes |
| コンパイル時間 | 249 ms |
| コンパイル使用メモリ | 81,864 KB |
| 実行使用メモリ | 70,388 KB |
| 最終ジャッジ日時 | 2025-06-12 20:38:31 |
| 合計ジャッジ時間 | 2,248 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 21 |
ソースコード
import math
import random
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 = math.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)
return factors
def factorize(n):
if n == 0:
return {}
factors = factor(n)
res = {}
for p in factors:
res[p] = res.get(p, 0) + 1
return res
def generate_divisors(factors):
divisors = [1]
for prime, exp in factors.items():
temp = []
for e in range(exp + 1):
prime_power = prime ** e
for d in divisors:
temp.append(d * prime_power)
divisors = temp
return divisors
def fib_mod(n, mod):
if mod == 1:
return 0
if n == 0:
return 0
a, b = 0, 1
for _ in range(n):
a, b = b, (a + b) % mod
return a
def compute_pisano_period(p):
if p == 2:
return 3
if p == 5:
return 20
mod = p
remainder = p % 5
if remainder == 1 or remainder == 4:
d = p - 1
else:
d = 2 * (p + 1)
if d == 0:
return 0
factors = factorize(d)
divisors = generate_divisors(factors)
divisors = list(set(divisors))
divisors.sort()
for m in divisors:
if m == 0:
continue
fm = fib_mod(m, mod)
fm1 = fib_mod(m + 1, mod)
if fm == 0 and fm1 == 1:
return m
return d
def lcm(a, b):
return a * b // math.gcd(a, b)
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
n = int(data[idx])
idx += 1
primes = []
for _ in range(n):
p = int(data[idx])
k = int(data[idx + 1])
primes.append((p, k))
idx += 2
pisano_periods = []
for p, k in primes:
if p == 2:
pi_p = 3
elif p == 5:
pi_p = 20
else:
pi_p = compute_pisano_period(p)
if k == 1:
pi_pk = pi_p
else:
pi_pk = pi_p * (p ** (k - 1))
pisano_periods.append(pi_pk)
current_lcm = 1
for period in pisano_periods:
current_lcm = lcm(current_lcm, period)
print(current_lcm % MOD)
if __name__ == "__main__":
main()
gew1fw