結果
問題 | No.3038 シャッフルの再現 |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:49:03 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,909 bytes |
コンパイル時間 | 418 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 68,864 KB |
最終ジャッジ日時 | 2025-03-26 15:50:07 |
合計ジャッジ時間 | 2,895 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 1 |
other | RE * 21 |
ソースコード
import sysimport randomfrom math import gcdMOD = 10**9 + 7def is_prime(n):if n < 2:return Falsefor p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:if n % p == 0:return n == pd = n - 1s = 0while d % 2 == 0:d //= 2s += 1for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:if a >= n:continuex = pow(a, d, n)if x == 1 or x == n - 1:continuefor _ in range(s - 1):x = pow(x, 2, n)if x == n - 1:breakelse:return Falsereturn Truedef pollards_rho(n):if n % 2 == 0:return 2if n % 3 == 0:return 3if n % 5 == 0:return 5while True:c = random.randint(1, n-1)f = lambda x: (pow(x, 2, n) + c) % nx, y, d = 2, 2, 1while d == 1:x = f(x)y = f(f(y))d = gcd(abs(x - y), n)if d != n:return ddef factor(n):factors = {}small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]for p in small_primes:if n % p == 0:cnt = 0while n % p == 0:cnt += 1n = n // pfactors[p] = cntif n == 1:return factorsif is_prime(n):factors[n] = 1return factorsdef _factor(n):if n == 1:return {}if is_prime(n):return {n: 1}d = pollards_rho(n)a = _factor(d)b = _factor(n // d)res = {}for p in a:res[p] = a[p]for p in b:if p in res:res[p] += b[p]else:res[p] = b[p]return resremaining_factors = _factor(n)for p in remaining_factors:factors[p] = factors.get(p, 0) + remaining_factors[p]return factorsdef generate_divisors(factors_dict):divisors = [1]for p in sorted(factors_dict.keys()):exp = factors_dict[p]current_powers = [p**e for e in range(exp+1)]new_divisors = []for d in divisors:for power in current_powers:new_divisors.append(d * power)divisors = list(set(new_divisors))divisors = sorted(divisors)return divisorsdef fib_pair(n, mod):if n == 0:return (0, 1)a, b = fib_pair(n // 2, mod)c = (a * (2 * b - a)) % modd = (a * a + b * b) % modif n % 2 == 0:return (c, d)else:return (d, (c + d) % mod)def compute_pisano_period(p):if p == 5:return 20mod5 = p % 5if mod5 in (1, 4):base = p - 1else:base = 2 * (p + 1)factors = factor(base)divisors = generate_divisors(factors)for d in sorted(divisors):if d == 0:continuefn, fn_plus_1 = fib_pair(d, p)if fn == 0 and fn_plus_1 == 1 % p:return dreturn basedef main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1lcm_factors = {}for _ in range(N):p = int(input[ptr])ptr += 1k = int(input[ptr])ptr += 1pisano_p = compute_pisano_period(p)factors_pisano = factor(pisano_p)if p in factors_pisano:factors_pisano[p] += (k - 1)else:factors_pisano[p] = (k - 1)for p_factor, exp in factors_pisano.items():if p_factor in lcm_factors:if exp > lcm_factors[p_factor]:lcm_factors[p_factor] = expelse:lcm_factors[p_factor] = expresult = 1for p, exp in lcm_factors.items():result = (result * pow(p, exp, MOD)) % MODprint(result)if __name__ == '__main__':main()