結果
問題 | No.308 素数は通れません |
ユーザー |
![]() |
提出日時 | 2020-01-02 17:54:35 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 33 ms / 1,000 ms |
コード長 | 1,003 bytes |
コンパイル時間 | 83 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-11-22 18:13:47 |
合計ジャッジ時間 | 5,459 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 107 |
ソースコード
import sysread = sys.stdin.buffer.readreadline = sys.stdin.buffer.readlinereadlines = sys.stdin.buffer.readlinesN = int(read())def MillerRabinTest(n, maxiter = 10):import randomif n == 1:return Falseelif n == 2:return Trueif not n&1:return Falsed = n - 1while not (d & 1):d >>= 1for _ in range(maxiter):a = random.randint(1,n-1)t = dx = pow(a,t,n)while (t != n-1) and (x != 1) and (x != n - 1):x = x * x % nt <<= 1if (x != n-1) and not (t & 1):return Falsereturn Truedef F(N):A = [0,0,0,0,3,0,5,0,7,7, # 0 ~ 97,0,11,0,13,7,7,0,8,0, # ~1919,19,7,0,23,23,8,8,8]if N < 30:return A[N]for x in [8,14]:if N % x != 1:return xif not MillerRabinTest(N - x):return xraise Exception # ないはずanswer = F(N)print(answer)