結果

問題 No.308 素数は通れません
ユーザー 0w1
提出日時 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
権限があれば一括ダウンロードができます

ソースコード

diff #

from random import randrange
from queue import Queue

def is_prime( n ):
  if n <= 1 or ( n > 2 and ~n & 1 ): return False
  if n == 2 or n == 3: return True
  for i in range( 100 ):
    a = randrange( 2, n - 1, 1 )
    p = n - 1
    if pow( a, p, n ) != 1: return False
    while ~p & 1:
      p >>= 1
      x = pow( a, p, n )
      if x == 1: continue
      if x == n - 1: break
      return False
  return True

N = 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 ] = True
    que.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: continue
        if di == 1 and u % w == 0: continue
        nu = u + dd[ di ]
        if not ( 1 <= nu and nu <= N ): continue
        if is_prime( nu ): continue
        if vis[ nu ]: continue
        vis[ nu ] = 1
        que.put( nu )
else:
  if N % 8 != 1 or not is_prime( N - 8 ):
    print( 8 )
  else:
    print( 14 )
0