結果
問題 | No.308 素数は通れません |
ユーザー | ciel |
提出日時 | 2015-12-02 03:03:15 |
言語 | Ruby (3.3.0) |
結果 |
AC
|
実行時間 | 97 ms / 1,000 ms |
コード長 | 1,557 bytes |
コンパイル時間 | 372 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 12,672 KB |
最終ジャッジ日時 | 2024-11-27 18:30:56 |
合計ジャッジ時間 | 12,089 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 88 ms
12,416 KB |
testcase_01 | AC | 87 ms
12,288 KB |
testcase_02 | AC | 89 ms
12,288 KB |
testcase_03 | AC | 97 ms
12,416 KB |
testcase_04 | AC | 89 ms
12,288 KB |
testcase_05 | AC | 88 ms
12,160 KB |
testcase_06 | AC | 90 ms
12,416 KB |
testcase_07 | AC | 89 ms
12,160 KB |
testcase_08 | AC | 88 ms
12,288 KB |
testcase_09 | AC | 91 ms
12,160 KB |
testcase_10 | AC | 90 ms
12,288 KB |
testcase_11 | AC | 88 ms
12,160 KB |
testcase_12 | AC | 88 ms
12,288 KB |
testcase_13 | AC | 91 ms
12,288 KB |
testcase_14 | AC | 90 ms
12,288 KB |
testcase_15 | AC | 90 ms
12,160 KB |
testcase_16 | AC | 88 ms
12,288 KB |
testcase_17 | AC | 89 ms
12,416 KB |
testcase_18 | AC | 89 ms
12,416 KB |
testcase_19 | AC | 91 ms
12,288 KB |
testcase_20 | AC | 88 ms
12,416 KB |
testcase_21 | AC | 90 ms
12,416 KB |
testcase_22 | AC | 90 ms
12,288 KB |
testcase_23 | AC | 91 ms
12,288 KB |
testcase_24 | AC | 92 ms
12,288 KB |
testcase_25 | AC | 91 ms
12,160 KB |
testcase_26 | AC | 89 ms
12,288 KB |
testcase_27 | AC | 91 ms
12,160 KB |
testcase_28 | AC | 90 ms
12,288 KB |
testcase_29 | AC | 93 ms
12,416 KB |
testcase_30 | AC | 90 ms
12,288 KB |
testcase_31 | AC | 88 ms
12,288 KB |
testcase_32 | AC | 87 ms
12,160 KB |
testcase_33 | AC | 87 ms
12,160 KB |
testcase_34 | AC | 89 ms
12,288 KB |
testcase_35 | AC | 89 ms
12,288 KB |
testcase_36 | AC | 86 ms
12,416 KB |
testcase_37 | AC | 86 ms
12,288 KB |
testcase_38 | AC | 90 ms
12,288 KB |
testcase_39 | AC | 92 ms
12,160 KB |
testcase_40 | AC | 86 ms
12,160 KB |
testcase_41 | AC | 87 ms
12,160 KB |
testcase_42 | AC | 88 ms
12,288 KB |
testcase_43 | AC | 91 ms
12,288 KB |
testcase_44 | AC | 90 ms
12,288 KB |
testcase_45 | AC | 91 ms
12,160 KB |
testcase_46 | AC | 88 ms
12,160 KB |
testcase_47 | AC | 88 ms
12,160 KB |
testcase_48 | AC | 89 ms
12,160 KB |
testcase_49 | AC | 90 ms
12,288 KB |
testcase_50 | AC | 94 ms
12,288 KB |
testcase_51 | AC | 88 ms
12,160 KB |
testcase_52 | AC | 89 ms
12,288 KB |
testcase_53 | AC | 88 ms
12,288 KB |
testcase_54 | AC | 92 ms
12,160 KB |
testcase_55 | AC | 96 ms
12,288 KB |
testcase_56 | AC | 96 ms
12,416 KB |
testcase_57 | AC | 95 ms
12,288 KB |
testcase_58 | AC | 95 ms
12,160 KB |
testcase_59 | AC | 90 ms
12,288 KB |
testcase_60 | AC | 89 ms
12,288 KB |
testcase_61 | AC | 90 ms
12,160 KB |
testcase_62 | AC | 90 ms
12,288 KB |
testcase_63 | AC | 93 ms
12,160 KB |
testcase_64 | AC | 91 ms
12,160 KB |
testcase_65 | AC | 91 ms
12,288 KB |
testcase_66 | AC | 91 ms
12,288 KB |
testcase_67 | AC | 92 ms
12,160 KB |
testcase_68 | AC | 92 ms
12,160 KB |
testcase_69 | AC | 94 ms
12,288 KB |
testcase_70 | AC | 93 ms
12,416 KB |
testcase_71 | AC | 92 ms
12,288 KB |
testcase_72 | AC | 90 ms
12,160 KB |
testcase_73 | AC | 90 ms
12,288 KB |
testcase_74 | AC | 88 ms
12,288 KB |
testcase_75 | AC | 92 ms
12,160 KB |
testcase_76 | AC | 90 ms
12,160 KB |
testcase_77 | AC | 89 ms
12,160 KB |
testcase_78 | AC | 93 ms
12,288 KB |
testcase_79 | AC | 89 ms
12,288 KB |
testcase_80 | AC | 90 ms
12,160 KB |
testcase_81 | AC | 88 ms
12,416 KB |
testcase_82 | AC | 87 ms
12,288 KB |
testcase_83 | AC | 86 ms
12,416 KB |
testcase_84 | AC | 92 ms
12,416 KB |
testcase_85 | AC | 90 ms
12,160 KB |
testcase_86 | AC | 94 ms
12,416 KB |
testcase_87 | AC | 89 ms
12,672 KB |
testcase_88 | AC | 86 ms
12,288 KB |
testcase_89 | AC | 89 ms
12,416 KB |
testcase_90 | AC | 90 ms
12,544 KB |
testcase_91 | AC | 88 ms
12,160 KB |
testcase_92 | AC | 90 ms
12,288 KB |
testcase_93 | AC | 89 ms
12,160 KB |
testcase_94 | AC | 88 ms
12,288 KB |
testcase_95 | AC | 90 ms
12,416 KB |
testcase_96 | AC | 88 ms
12,160 KB |
testcase_97 | AC | 88 ms
12,288 KB |
testcase_98 | AC | 87 ms
12,288 KB |
testcase_99 | AC | 88 ms
12,160 KB |
testcase_100 | AC | 90 ms
12,416 KB |
testcase_101 | AC | 90 ms
12,544 KB |
testcase_102 | AC | 87 ms
12,288 KB |
testcase_103 | AC | 87 ms
12,416 KB |
testcase_104 | AC | 89 ms
12,288 KB |
testcase_105 | AC | 93 ms
12,288 KB |
testcase_106 | AC | 95 ms
12,288 KB |
コンパイルメッセージ
Syntax OK
ソースコード
#!/usr/bin/ruby require 'prime' # https://github.com/ChapterHouse/prime_miller_rabin # Copyright (c) 2013 Frank Hall # MIT License class Prime::MillerRabin < Prime::PseudoPrimeGenerator def succ() self.last_prime = next_prime(last_prime || 1) end def rewind() self.last_prime = nil end def prime?(x) miller_rabin(x) end private attr_accessor :last_prime def likely_prime?(a, n) d = n - 1 s = 0 while d % 2 == 0 do d >>= 1 s += 1 end b = 1 while d > 0 u = d % 2 t = d / 2 b = (b * a) % n if u == 1 a = a**2 % n d = t end if b == 1 true else s.times do |i| return true if b == n - 1 b = (b * b) % n end (b == n - 1) end end def miller_rabin(n) if n.abs < 2 false else likely_prime = true # 26 Yields a probability of prime at 99.99999999999998% so lets kick it up a notch. 27.times do |i| begin a = rand(n) end while a == 0 likely_prime = likely_prime?(a, n) break unless likely_prime end likely_prime end end def next_prime(x) if x < 2 2 elsif x < 3 3 elsif x < 5 5 else x += (x.even? ? 1 : (x % 10 == 3 ? 4 : 2 )) x += (x % 10 == 3 ? 4 : 2 ) until x.prime? x end end end N=gets.to_i if N>25 p N%8==1&&Prime::MillerRabin.new.prime?(N-8) ? 14 : 8 else h={4=>3,6=>5,12=>11,14=>13,18=>8,20=>19,21=>19,24=>23,25=>23};h.default=7 p h[N] end