結果

問題 No.308 素数は通れません
ユーザー tubo28tubo28
提出日時 2015-12-01 21:02:58
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 671 bytes
コンパイル時間 122 ms
コンパイル使用メモリ 11,576 KB
実行使用メモリ 126,084 KB
最終ジャッジ日時 2023-10-12 06:55:15
合計ジャッジ時間 11,774 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
19,660 KB
testcase_01 AC 90 ms
15,560 KB
testcase_02 AC 89 ms
15,116 KB
testcase_03 AC 90 ms
15,124 KB
testcase_04 AC 94 ms
15,360 KB
testcase_05 AC 89 ms
15,168 KB
testcase_06 AC 89 ms
15,348 KB
testcase_07 AC 89 ms
15,216 KB
testcase_08 AC 88 ms
15,212 KB
testcase_09 AC 88 ms
15,248 KB
testcase_10 AC 88 ms
15,372 KB
testcase_11 AC 89 ms
15,328 KB
testcase_12 AC 90 ms
15,200 KB
testcase_13 AC 90 ms
15,220 KB
testcase_14 AC 90 ms
15,372 KB
testcase_15 AC 89 ms
15,148 KB
testcase_16 AC 92 ms
15,368 KB
testcase_17 AC 88 ms
15,108 KB
testcase_18 AC 95 ms
15,200 KB
testcase_19 AC 92 ms
15,260 KB
testcase_20 AC 90 ms
15,236 KB
testcase_21 AC 92 ms
15,352 KB
testcase_22 AC 90 ms
15,308 KB
testcase_23 AC 89 ms
15,248 KB
testcase_24 AC 91 ms
15,476 KB
testcase_25 AC 90 ms
15,472 KB
testcase_26 AC 90 ms
15,108 KB
testcase_27 AC 90 ms
15,364 KB
testcase_28 AC 90 ms
15,368 KB
testcase_29 AC 91 ms
15,112 KB
testcase_30 AC 92 ms
15,160 KB
testcase_31 AC 92 ms
15,100 KB
testcase_32 AC 90 ms
15,240 KB
testcase_33 AC 91 ms
15,204 KB
testcase_34 AC 90 ms
15,308 KB
testcase_35 AC 89 ms
15,212 KB
testcase_36 AC 91 ms
15,676 KB
testcase_37 AC 91 ms
15,512 KB
testcase_38 AC 96 ms
15,408 KB
testcase_39 AC 90 ms
15,316 KB
testcase_40 AC 92 ms
15,360 KB
testcase_41 AC 91 ms
15,588 KB
testcase_42 AC 99 ms
15,428 KB
testcase_43 AC 92 ms
15,512 KB
testcase_44 AC 91 ms
15,416 KB
testcase_45 AC 92 ms
15,568 KB
testcase_46 AC 91 ms
15,420 KB
testcase_47 AC 95 ms
15,432 KB
testcase_48 AC 94 ms
15,564 KB
testcase_49 AC 96 ms
15,848 KB
testcase_50 AC 96 ms
15,620 KB
testcase_51 AC 101 ms
15,664 KB
testcase_52 AC 104 ms
15,792 KB
testcase_53 AC 102 ms
15,748 KB
testcase_54 AC 102 ms
16,156 KB
testcase_55 AC 100 ms
16,000 KB
testcase_56 AC 241 ms
25,356 KB
testcase_57 AC 245 ms
25,744 KB
testcase_58 TLE -
testcase_59 TLE -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
testcase_101 -- -
testcase_102 -- -
testcase_103 -- -
testcase_104 -- -
testcase_105 -- -
testcase_106 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require "prime"

def ok n,w
  stack = []
  vis = n.times.map { false }
  stack.push 1
  while stack.size != 0
    c = stack.pop
    return true if c == n
    vis[c] = true
    if c%w != 0 && !vis[c+1] && !$isprime[c+1]
      stack.push c+1
    end
    if c%w != 1 && !vis[c-1] && !$isprime[c-1]
      stack.push c-1
    end
    if c-w >= 1 && !vis[c-w] && !$isprime[c-w]
      stack.push c-w
    end
    if c+w <= n && !vis[c+w] && !$isprime[c+w]
      stack.push c+w
    end
  end
  false
end

n = gets.to_i

$isprime = n.times.map { false }
Prime.each(n) { |p| $isprime[p] = true }

n.times.each do |w|
  next if w < 3
  if ok n,w
    puts w
    exit
  end
end

puts n
0