結果
問題 |
No.2785 四乗足す四の末尾の0
|
ユーザー |
![]() |
提出日時 | 2025-03-17 22:13:40 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,144 bytes |
コンパイル時間 | 485 ms |
コンパイル使用メモリ | 82,356 KB |
実行使用メモリ | 84,648 KB |
最終ジャッジ日時 | 2025-03-17 22:13:46 |
合計ジャッジ時間 | 5,589 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 TLE * 1 -- * 7 |
ソースコード
from collections import defaultdict def main(): def test_case(N): M = N**4 + 4 print('Yes' if is_prime(M) else 'No') pfactors = factorize(M) ans = min(pfactors[2], pfactors[5]) print(ans) T = int(input()) for _ in range(T): N = int(input()) test_case(N) def is_prime(n): if n == 2: return True elif n == 1 or n % 2 == 0: return False m = n - 1 lsb = m & -m s = lsb.bit_length() - 1 d = m // lsb for a in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37): if a == n: continue x = pow(a, d, n) r = 0 if x == 1: continue while x != m: x = pow(x, 2, n) r += 1 if x == 1 or r == s: return False return True def gcd(n, m): while n: n, m = m%n, n return m def prime_factor(n): if n % 2 == 0: return 2 m = int(n ** 0.125) + 1 for c in range(1, n): f = lambda a: (pow(a, 2, n) + c) % n y = 0 g = q = r = 1 k = 0 while g == 1: x = y while k < 3 * r // 4: y = f(y) k += 1 while k < r and g == 1: ys = y for _ in range(min(m, r - k)): y = f(y) q = q * abs(x - y) % n g = gcd(q, n) k += m k = r r *= 2 if g == n: g = 1 y = ys while g == 1: y = f(y) g = gcd(abs(x - y), n) if g == n: continue if is_prime(g): return g elif is_prime(n // g): return n // g else: return prime_factor(g) def factorize(n): res = defaultdict(lambda: 0) while not is_prime(n) and n > 1: p = prime_factor(n) s = 0 while n % p == 0: n //= p s += 1 res[p] = s if n > 1: res[n] = 1 return res if __name__ == '__main__': main()