結果

問題 No.308 素数は通れません
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 01:53:37
言語 Ruby
(3.2.2)
結果
AC  
実行時間 83 ms / 1,000 ms
コード長 1,089 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 11,208 KB
実行使用メモリ 15,368 KB
最終ジャッジ日時 2023-08-18 12:54:48
合計ジャッジ時間 11,514 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
15,032 KB
testcase_01 AC 74 ms
15,128 KB
testcase_02 AC 74 ms
15,048 KB
testcase_03 AC 73 ms
15,144 KB
testcase_04 AC 77 ms
15,128 KB
testcase_05 AC 77 ms
15,128 KB
testcase_06 AC 75 ms
15,164 KB
testcase_07 AC 72 ms
15,260 KB
testcase_08 AC 71 ms
15,324 KB
testcase_09 AC 73 ms
15,088 KB
testcase_10 AC 72 ms
15,176 KB
testcase_11 AC 71 ms
15,256 KB
testcase_12 AC 75 ms
15,268 KB
testcase_13 AC 75 ms
15,292 KB
testcase_14 AC 72 ms
15,272 KB
testcase_15 AC 75 ms
15,308 KB
testcase_16 AC 73 ms
15,032 KB
testcase_17 AC 72 ms
15,148 KB
testcase_18 AC 71 ms
15,292 KB
testcase_19 AC 72 ms
15,112 KB
testcase_20 AC 76 ms
15,364 KB
testcase_21 AC 74 ms
15,120 KB
testcase_22 AC 73 ms
15,116 KB
testcase_23 AC 74 ms
15,296 KB
testcase_24 AC 71 ms
15,288 KB
testcase_25 AC 74 ms
15,108 KB
testcase_26 AC 73 ms
15,104 KB
testcase_27 AC 73 ms
15,304 KB
testcase_28 AC 73 ms
15,124 KB
testcase_29 AC 75 ms
15,240 KB
testcase_30 AC 74 ms
15,168 KB
testcase_31 AC 73 ms
15,080 KB
testcase_32 AC 73 ms
15,340 KB
testcase_33 AC 74 ms
15,244 KB
testcase_34 AC 69 ms
15,236 KB
testcase_35 AC 71 ms
15,108 KB
testcase_36 AC 69 ms
15,220 KB
testcase_37 AC 69 ms
15,296 KB
testcase_38 AC 69 ms
15,148 KB
testcase_39 AC 71 ms
15,324 KB
testcase_40 AC 72 ms
15,088 KB
testcase_41 AC 71 ms
15,036 KB
testcase_42 AC 68 ms
15,184 KB
testcase_43 AC 71 ms
15,164 KB
testcase_44 AC 74 ms
15,048 KB
testcase_45 AC 72 ms
15,288 KB
testcase_46 AC 75 ms
15,116 KB
testcase_47 AC 83 ms
15,132 KB
testcase_48 AC 71 ms
15,292 KB
testcase_49 AC 70 ms
15,044 KB
testcase_50 AC 71 ms
15,232 KB
testcase_51 AC 71 ms
15,164 KB
testcase_52 AC 68 ms
15,092 KB
testcase_53 AC 73 ms
15,156 KB
testcase_54 AC 72 ms
15,092 KB
testcase_55 AC 70 ms
15,368 KB
testcase_56 AC 74 ms
15,040 KB
testcase_57 AC 73 ms
15,044 KB
testcase_58 AC 74 ms
15,044 KB
testcase_59 AC 69 ms
15,272 KB
testcase_60 AC 72 ms
15,116 KB
testcase_61 AC 72 ms
15,304 KB
testcase_62 AC 70 ms
15,344 KB
testcase_63 AC 73 ms
15,092 KB
testcase_64 AC 74 ms
15,172 KB
testcase_65 AC 72 ms
15,308 KB
testcase_66 AC 73 ms
15,152 KB
testcase_67 AC 72 ms
15,276 KB
testcase_68 AC 76 ms
15,324 KB
testcase_69 AC 74 ms
15,040 KB
testcase_70 AC 74 ms
15,128 KB
testcase_71 AC 72 ms
15,180 KB
testcase_72 AC 72 ms
15,092 KB
testcase_73 AC 72 ms
15,260 KB
testcase_74 AC 73 ms
15,256 KB
testcase_75 AC 74 ms
15,132 KB
testcase_76 AC 73 ms
15,096 KB
testcase_77 AC 75 ms
15,252 KB
testcase_78 AC 75 ms
15,148 KB
testcase_79 AC 75 ms
15,112 KB
testcase_80 AC 72 ms
15,180 KB
testcase_81 AC 75 ms
15,168 KB
testcase_82 AC 71 ms
15,152 KB
testcase_83 AC 70 ms
15,044 KB
testcase_84 AC 76 ms
15,040 KB
testcase_85 AC 75 ms
15,164 KB
testcase_86 AC 74 ms
15,292 KB
testcase_87 AC 70 ms
15,116 KB
testcase_88 AC 72 ms
15,300 KB
testcase_89 AC 73 ms
15,148 KB
testcase_90 AC 75 ms
15,144 KB
testcase_91 AC 71 ms
15,144 KB
testcase_92 AC 75 ms
15,092 KB
testcase_93 AC 76 ms
15,276 KB
testcase_94 AC 73 ms
15,148 KB
testcase_95 AC 68 ms
15,132 KB
testcase_96 AC 71 ms
15,308 KB
testcase_97 AC 70 ms
15,036 KB
testcase_98 AC 70 ms
15,308 KB
testcase_99 AC 69 ms
15,164 KB
testcase_100 AC 72 ms
15,296 KB
testcase_101 AC 73 ms
15,288 KB
testcase_102 AC 70 ms
15,152 KB
testcase_103 AC 74 ms
15,148 KB
testcase_104 AC 71 ms
15,288 KB
testcase_105 AC 71 ms
15,152 KB
testcase_106 AC 70 ms
15,164 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

#wikipedia より 
#https://ja.wikipedia.org/wiki/%E3%83%A9%E3%83%93%E3%83%B3-%E3%83%9F%E3%83%A9%E3%83%BC%E7%B4%A0%E6%95%B0%E5%88%A4%E5%AE%9A%E6%B3%95
def prime?(n)
  return true if n == 2
  return false if n == 1 || n & 1 == 0
  d = n-1
  d >>= 1 while d & 1 == 0
  20.times do                               # 20 は上の説明の k に相当
    a = rand(n-2) + 1
    t = d
    y = ModMath.pow(a,t,n)                  # 実装コードは下にある
    while t != n-1 && y != 1 && y != n-1
      y = (y * y) % n
      t <<= 1
    end
    return false if y != n-1 && t & 1 == 0
  end
  return true
end

module ModMath
	def ModMath.pow(base, power, mod)
	  result = 1
	  while power > 0
	    result = (result * base) % mod if power & 1 == 1
	    base = (base * base) % mod
	    power >>= 1;
	  end
	  result
	end
end


def func(n)
	arr = [-1,-1,-1,-1,3,0,5,0,7,7,7,0,11,0,13,7,7,0,8,0,19,19,7,0,23,23,8,8,8,0,8,0,8,8,8,8,8,0,8,8,8,0,8,0,8,8,8,0,-1]

	if(n<48) then
		return arr[n]
	end
	if n%8==1 && prime?(n-8) then 
		return 14
	else
		return 8
	end
end


puts func(n)
0