結果
| 問題 |
No.3038 シャッフルの再現
|
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 15:33:38 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 4,022 bytes |
| コンパイル時間 | 275 ms |
| コンパイル使用メモリ | 82,316 KB |
| 実行使用メモリ | 70,996 KB |
| 最終ジャッジ日時 | 2025-06-12 15:34:57 |
| 合計ジャッジ時間 | 2,348 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 21 |
ソースコード
import sys
import random
import math
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)
factors.sort()
return factors
def factorize(n):
if n == 1:
return {}
factors = factor(n)
res = {}
for p in factors:
if p in res:
res[p] += 1
else:
res[p] = 1
return res
def generate_divisors(factors):
divisors = [1]
for p in factors:
exponents = [p**e for e in range(1, factors[p]+1)]
new_divisors = []
for d in divisors:
for exp in exponents:
new_divisors.append(d * exp)
divisors += new_divisors
divisors = list(set(divisors))
divisors.sort()
return divisors
def matrix_mult(a, b, mod):
return [
[(a[0][0]*b[0][0] + a[0][1]*b[1][0]) % mod,
(a[0][0]*b[0][1] + a[0][1]*b[1][1]) % mod],
[(a[1][0]*b[0][0] + a[1][1]*b[1][0]) % mod,
(a[1][0]*b[0][1] + a[1][1]*b[1][1]) % mod]
]
def matrix_pow(mat, power, mod):
result = [[1, 0], [0, 1]]
while power > 0:
if power % 2 == 1:
result = matrix_mult(result, mat, mod)
mat = matrix_mult(mat, mat, mod)
power = power // 2
return result
def compute_fib_d(d, p):
if d == 0:
return (0, 1)
mat = [[1, 1], [1, 0]]
mat_d = matrix_pow(mat, d, p)
fib_d = mat_d[1][0]
fib_d_plus1 = mat_d[0][0]
return (fib_d % p, fib_d_plus1 % p)
def compute_pisano(p):
if p == 2:
return 3
if p == 5:
return 20
mod = p % 5
if mod == 1 or mod == 4:
n = p - 1
else:
n = 2 * (p + 1)
if n == 0:
return 1
factors = factorize(n)
divisors = generate_divisors(factors)
for d in divisors:
if d == 0:
continue
fib_d, fib_d_plus1 = compute_fib_d(d, p)
if fib_d == 0 and fib_d_plus1 == 1:
return d
return n
def main():
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx +=1
primes = []
for _ in range(N):
p = int(input[idx])
k = int(input[idx+1])
idx +=2
primes.append( (p, k) )
pisano_periods = []
for p, k in primes:
if p == 2:
period = 3 * (2 ** (k -1))
elif p ==5:
period = 20 * (5 ** (k-1))
else:
if not is_prime(p):
raise ValueError("p must be prime.")
base = compute_pisano(p)
period = base * (p ** (k-1))
pisano_periods.append( period )
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
result = 1
for period in pisano_periods:
result = lcm(result, period)
result %= MOD
print(result)
if __name__ == '__main__':
main()
gew1fw