結果

問題 No.308 素数は通れません
ユーザー nmnmnmnmnmnmnm
提出日時 2015-12-01 10:43:57
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 1,047 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 7,200 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-14 06:52:43
合計ジャッジ時間 6,696 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 105 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

x = input()

def isPrime(n):
  if n < 2:
    return 0
  elif n == 2:
    return 1
  for i in range(2, int(math.sqrt(n))+1):
    if not n % i:
      return 0
  return 1
  
def isPrime2(q,k=10000):
    q = abs(q)
    if q == 2: return 1
    if q < 2 or q&1 == 0: return 0
    for i in xrange(3,k):
        x,y = q,i
        while y:
            x, y =  y, x % y
        if x != 1: continue
        if pow(i, q-1, q) != 1:
            return 0
    return 1

def gcd(a, b):
  if b == 0: return a
  return gcd(b, a % b)

d=[-1,-1,-1,-1,3,-1,5,-1,7,7,7,-1,11,-1,13,7,7,-1,8,-1,19,19,7,-1,23,23,8,8,8,-1,8,-1,8,8,8,8,8,-1,8,8,8,-1,8,-1,8,8,8,-1,8,14,8,8,8,-1,8,8,8,8,8,-1,8,-1,8,8,8,8,8,-1,8,8,8,-1,8,-1,8,8,8,8,8,-1,8,14,8,-1,8,8,8,8,8,-1,8,8,8,8,8,8,8,-1,8,8]

if x < 100:
    print d[x]
else:
    a = x
    b = x-8
    c = x-1
    if (a-1)%8==0:
        if isPrime2(b)==1:
            print 14
        else:
            print 8
    else:
        if isPrime2(b)==1 and isPrime2(c)==1:
            print 14
        else:
            print 8
0