結果
問題 | No.36 素数が嫌い! |
ユーザー |
![]() |
提出日時 | 2014-10-29 06:31:39 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 37 ms / 5,000 ms |
コード長 | 1,809 bytes |
コンパイル時間 | 118 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,392 KB |
最終ジャッジ日時 | 2024-06-27 00:12:02 |
合計ジャッジ時間 | 2,006 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
import sys import random def miller_rabin_pass(base, s, d, n, pow=pow): base = pow(base, d, n) if base == 1 or base == n - 1: return True for _ in range(s - 1): base = base * base % n if base == n - 1: return True return False def miller_rabin(n): if n <= 1: return False if n <= 3: return True d = n - 1 s = d & -d s = s.bit_length() - 1 d >>= s if n < 316349281: if n < 1373653: bases = [2, 3] else: bases = [11000544, 31481107] elif n < 154639673381: if n < 4759123141: bases = [2, 7, 61] else: bases = [15, 176006322, 4221622697] elif n < 47636622961201: bases = [2, 2570940, 211991001, 3749873356] elif n < 3770579582154547: bases = [2, 2570940, 880937, 610386380, 4130785767] elif n < (1 << 64): bases = [2, 325, 9375, 28178, 450775, 9780504, 1795265022] else: bases = [random.randrange(n - 1) + 1 for _ in range(30)] for base in bases: if not miller_rabin_pass(base, s, d, n): return False return True def solve(): stdin = sys.stdin N = int(stdin.readline()) e = 0 while not N & 1: N >>= 1 e += 1 ok = False if e >= 3: ok = True elif e >= 2 and N > 1: ok = True elif e >= 1 and N > 1: ok = not miller_rabin(N) elif N > 1: th = int(pow(N, 0.33333334)) for i in range(3, th + 1, 2): if N % i == 0: N //= i e += 1 while N % i == 0: N //= i e += 1 if e >= 3 or (e >= 2 and N > 1): ok = True break elif N > 1: if not miller_rabin(N): ok = True break else: break print("YES" if ok else "NO") solve()