結果

問題 No.12 限定された素数
ユーザー yaoshimaxyaoshimax
提出日時 2015-02-10 11:49:57
言語 Python2
(2.7.18)
結果
AC  
実行時間 2,879 ms / 5,000 ms
コード長 1,370 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 131,860 KB
最終ジャッジ日時 2024-05-03 09:03:24
合計ジャッジ時間 57,401 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,834 ms
131,728 KB
testcase_01 AC 1,968 ms
131,604 KB
testcase_02 AC 1,876 ms
131,836 KB
testcase_03 AC 2,879 ms
131,804 KB
testcase_04 AC 1,934 ms
131,836 KB
testcase_05 AC 2,053 ms
131,728 KB
testcase_06 AC 2,400 ms
131,732 KB
testcase_07 AC 2,364 ms
131,860 KB
testcase_08 AC 2,028 ms
131,732 KB
testcase_09 AC 2,019 ms
131,604 KB
testcase_10 AC 1,957 ms
131,604 KB
testcase_11 AC 2,619 ms
131,604 KB
testcase_12 AC 2,386 ms
131,764 KB
testcase_13 AC 2,283 ms
131,604 KB
testcase_14 AC 1,990 ms
131,816 KB
testcase_15 AC 2,253 ms
131,732 KB
testcase_16 AC 2,436 ms
131,732 KB
testcase_17 AC 1,867 ms
131,604 KB
testcase_18 AC 1,882 ms
131,604 KB
testcase_19 AC 1,850 ms
131,600 KB
testcase_20 AC 1,913 ms
131,708 KB
testcase_21 AC 2,012 ms
131,728 KB
testcase_22 AC 1,893 ms
131,856 KB
testcase_23 AC 1,910 ms
131,704 KB
testcase_24 AC 1,883 ms
131,604 KB
testcase_25 AC 2,082 ms
131,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(raw_input())
A=map(int,raw_input().split())

isOK=[False for i in range(10)]
for a in A:
   isOK[a]=True


upperBound = 5000001
isPrime=[True for i in range((upperBound-3)/2+1)]
for i in range((upperBound-3)/2+1):
   if isPrime[i]:
      k = i+i+3 # this is prime number
      # false out non_prime number
      # k*k = 4i^2+12i+9 = 2*(2i^2+6i+3)+3
      # k*(k+1) = 2*(2i^2+6i+3)+3 + 2i+3 ... even number. skip
      # k*(k+2) = 2*(2i^2+6i+3+k)+3 
      non_prime = k*(i+1)+i
      for j in range(non_prime,(upperBound-3)/2+1,k):
         isPrime[j]=False
primes = [2] + [i+i+3 for i in range((upperBound-3)/2+1) if isPrime[i]==True]
primes = [0]+primes+[upperBound]
#print len(primes)
ans = -1
i = 1
while i < len(primes)-1:
   p=primes[i]
   ok=True
   s= set([])
   #print primes[i]
   while p != 0:
      if isOK[p%10] == False :
         ok=False
         break
      s.add(p%10)
      p/=10
   if ok==False:
      i+=1
      continue
   j = i+1
   while j < len(primes)-1:
      p = primes[j]
      ok2=True
      while p != 0:
         if isOK[p%10] == False:
            ok2=False
            break
         p/=10
      if ok2== False:
         break
      p=primes[j]
      while p != 0:
         s.add(p%10)
         p/=10
      j+=1
   if len(s)==N:
      ans = max(ans, primes[j]-1-(primes[i-1]+1) )
   #print primes[i-1],primes[j]
   i=j+1
print ans
0