結果
問題 | No.308 素数は通れません |
ユーザー |
|
提出日時 | 2017-04-15 20:52:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 103 ms / 1,000 ms |
コード長 | 1,093 bytes |
コンパイル時間 | 439 ms |
コンパイル使用メモリ | 82,340 KB |
実行使用メモリ | 78,720 KB |
最終ジャッジ日時 | 2024-11-27 18:54:01 |
合計ジャッジ時間 | 10,874 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 107 |
ソースコード
from random import randrangefrom queue import Queuedef is_prime( n ):if n <= 1 or ( n > 2 and ~n & 1 ): return Falseif n == 2 or n == 3: return Truefor i in range( 100 ):a = randrange( 2, n - 1, 1 )p = n - 1if pow( a, p, n ) != 1: return Falsewhile ~p & 1:p >>= 1x = pow( a, p, n )if x == 1: continueif x == n - 1: breakreturn Falsereturn TrueN = int( input() )if N < 100:for w in range( 1, N + 1, 1 ):dd = [ -1, 1, -w, w ]vis = [ False for i in range( N + 1 ) ]que = Queue()vis[ 1 ] = Trueque.put( 1 )while not que.empty():u = que.get()if u == N:exit( print( w ) )for di in range( 4 ):if di == 0 and ( u - 1 ) % w == 0: continueif di == 1 and u % w == 0: continuenu = u + dd[ di ]if not ( 1 <= nu and nu <= N ): continueif is_prime( nu ): continueif vis[ nu ]: continuevis[ nu ] = 1que.put( nu )else:if N % 8 != 1 or not is_prime( N - 8 ):print( 8 )else:print( 14 )