結果

問題 No.308 素数は通れません
ユーザー kura07kura07
提出日時 2015-12-01 02:16:06
言語 Ruby
(3.2.2)
結果
AC  
実行時間 86 ms / 1,000 ms
コード長 873 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 11,316 KB
実行使用メモリ 15,548 KB
最終ジャッジ日時 2023-08-18 12:56:06
合計ジャッジ時間 12,185 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,312 KB
testcase_01 AC 81 ms
15,364 KB
testcase_02 AC 78 ms
15,348 KB
testcase_03 AC 82 ms
15,184 KB
testcase_04 AC 84 ms
15,376 KB
testcase_05 AC 79 ms
15,372 KB
testcase_06 AC 81 ms
15,428 KB
testcase_07 AC 85 ms
15,332 KB
testcase_08 AC 85 ms
15,384 KB
testcase_09 AC 82 ms
15,232 KB
testcase_10 AC 82 ms
15,452 KB
testcase_11 AC 81 ms
15,140 KB
testcase_12 AC 79 ms
15,192 KB
testcase_13 AC 83 ms
15,308 KB
testcase_14 AC 82 ms
15,176 KB
testcase_15 AC 79 ms
15,152 KB
testcase_16 AC 83 ms
15,176 KB
testcase_17 AC 77 ms
15,316 KB
testcase_18 AC 80 ms
15,104 KB
testcase_19 AC 80 ms
15,216 KB
testcase_20 AC 80 ms
15,108 KB
testcase_21 AC 81 ms
15,312 KB
testcase_22 AC 77 ms
15,312 KB
testcase_23 AC 84 ms
15,288 KB
testcase_24 AC 80 ms
15,104 KB
testcase_25 AC 78 ms
15,184 KB
testcase_26 AC 84 ms
15,348 KB
testcase_27 AC 79 ms
15,364 KB
testcase_28 AC 77 ms
15,388 KB
testcase_29 AC 82 ms
15,220 KB
testcase_30 AC 86 ms
15,348 KB
testcase_31 AC 84 ms
15,364 KB
testcase_32 AC 78 ms
15,188 KB
testcase_33 AC 79 ms
15,160 KB
testcase_34 AC 81 ms
15,208 KB
testcase_35 AC 82 ms
15,244 KB
testcase_36 AC 79 ms
15,368 KB
testcase_37 AC 78 ms
15,236 KB
testcase_38 AC 80 ms
15,240 KB
testcase_39 AC 85 ms
15,420 KB
testcase_40 AC 85 ms
15,352 KB
testcase_41 AC 81 ms
15,296 KB
testcase_42 AC 78 ms
15,236 KB
testcase_43 AC 83 ms
15,216 KB
testcase_44 AC 82 ms
15,364 KB
testcase_45 AC 80 ms
15,180 KB
testcase_46 AC 77 ms
15,188 KB
testcase_47 AC 79 ms
15,356 KB
testcase_48 AC 77 ms
15,308 KB
testcase_49 AC 78 ms
15,192 KB
testcase_50 AC 85 ms
15,096 KB
testcase_51 AC 78 ms
15,104 KB
testcase_52 AC 81 ms
15,224 KB
testcase_53 AC 76 ms
15,104 KB
testcase_54 AC 79 ms
15,196 KB
testcase_55 AC 80 ms
15,256 KB
testcase_56 AC 81 ms
15,156 KB
testcase_57 AC 77 ms
15,104 KB
testcase_58 AC 77 ms
15,172 KB
testcase_59 AC 79 ms
15,288 KB
testcase_60 AC 80 ms
15,324 KB
testcase_61 AC 76 ms
15,176 KB
testcase_62 AC 79 ms
15,216 KB
testcase_63 AC 77 ms
15,156 KB
testcase_64 AC 82 ms
15,228 KB
testcase_65 AC 81 ms
15,188 KB
testcase_66 AC 80 ms
15,212 KB
testcase_67 AC 78 ms
15,244 KB
testcase_68 AC 77 ms
15,148 KB
testcase_69 AC 76 ms
15,244 KB
testcase_70 AC 75 ms
15,220 KB
testcase_71 AC 77 ms
15,184 KB
testcase_72 AC 81 ms
15,160 KB
testcase_73 AC 81 ms
15,144 KB
testcase_74 AC 78 ms
15,148 KB
testcase_75 AC 82 ms
15,216 KB
testcase_76 AC 83 ms
15,348 KB
testcase_77 AC 80 ms
15,400 KB
testcase_78 AC 80 ms
15,220 KB
testcase_79 AC 80 ms
15,216 KB
testcase_80 AC 78 ms
15,352 KB
testcase_81 AC 80 ms
15,548 KB
testcase_82 AC 80 ms
15,272 KB
testcase_83 AC 77 ms
15,304 KB
testcase_84 AC 80 ms
15,384 KB
testcase_85 AC 77 ms
15,220 KB
testcase_86 AC 81 ms
15,536 KB
testcase_87 AC 84 ms
15,388 KB
testcase_88 AC 84 ms
15,212 KB
testcase_89 AC 76 ms
15,296 KB
testcase_90 AC 80 ms
15,384 KB
testcase_91 AC 76 ms
15,176 KB
testcase_92 AC 81 ms
15,196 KB
testcase_93 AC 79 ms
15,104 KB
testcase_94 AC 76 ms
15,196 KB
testcase_95 AC 77 ms
15,312 KB
testcase_96 AC 76 ms
15,236 KB
testcase_97 AC 76 ms
15,364 KB
testcase_98 AC 79 ms
15,316 KB
testcase_99 AC 77 ms
15,220 KB
testcase_100 AC 77 ms
15,376 KB
testcase_101 AC 82 ms
15,396 KB
testcase_102 AC 81 ms
15,184 KB
testcase_103 AC 82 ms
15,236 KB
testcase_104 AC 81 ms
15,364 KB
testcase_105 AC 80 ms
15,352 KB
testcase_106 AC 78 ms
15,392 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'prime'

class Integer
	def miller_rabin_prime?(k=100)
		n = abs
		return true if n == 2
		return false if n < 2 || n % 2 == 0
		d = n-1
		d /= 2 while d % 2 == 0
		k.times{
			a = rand(1..n-1)
			t = d
			y = Integer.mod_pow(a, t, n)
			while t != n-1 && y != 1 && y != n-1
				y = y ** 2 % n
				t *= 2
			end
			return false if y != n-1 && t % 2 == 0
		}
		return true
	end
	def self.mod_pow(base, pow, mod)
		result = 1
		while pow > 0
			result = result * base % mod if pow % 2 == 1
			base = base ** 2 % mod
			pow /= 2
		end
		return result
	end
end

h = {
    4 => 3,
    6 => 5,
    8 => 7,
    9 => 7,
    10 => 7,
    12 => 11,
    14 => 13,
    15 => 7,
    16 => 7,
    18 => 8,
    20 => 19,
    21 => 19,
    22 => 7,
    24 => 23,
    25 => 23
}

n = gets.to_i
if h[n]
	puts h[n]
elsif n%8 == 1 && (n-8).miller_rabin_prime?
	puts 14
else
	puts 8
end
0