結果

問題 No.308 素数は通れません
ユーザー rickythetarickytheta
提出日時 2015-12-02 00:20:37
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,644 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 11,276 KB
実行使用メモリ 15,940 KB
最終ジャッジ日時 2023-10-12 08:20:01
合計ジャッジ時間 16,071 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
15,364 KB
testcase_01 AC 97 ms
15,524 KB
testcase_02 AC 102 ms
15,592 KB
testcase_03 AC 104 ms
15,428 KB
testcase_04 WA -
testcase_05 AC 110 ms
15,456 KB
testcase_06 AC 115 ms
15,596 KB
testcase_07 AC 102 ms
15,640 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 124 ms
15,568 KB
testcase_11 AC 124 ms
15,476 KB
testcase_12 WA -
testcase_13 AC 132 ms
15,448 KB
testcase_14 AC 131 ms
15,428 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 92 ms
15,488 KB
testcase_37 AC 92 ms
15,444 KB
testcase_38 AC 93 ms
15,516 KB
testcase_39 AC 94 ms
15,556 KB
testcase_40 AC 93 ms
15,416 KB
testcase_41 AC 93 ms
15,344 KB
testcase_42 AC 94 ms
15,420 KB
testcase_43 AC 92 ms
15,392 KB
testcase_44 AC 93 ms
15,412 KB
testcase_45 AC 94 ms
15,440 KB
testcase_46 AC 92 ms
15,476 KB
testcase_47 AC 93 ms
15,388 KB
testcase_48 AC 95 ms
15,416 KB
testcase_49 AC 94 ms
15,480 KB
testcase_50 AC 94 ms
15,408 KB
testcase_51 AC 95 ms
15,440 KB
testcase_52 AC 95 ms
15,412 KB
testcase_53 AC 94 ms
15,340 KB
testcase_54 AC 89 ms
15,388 KB
testcase_55 AC 89 ms
15,508 KB
testcase_56 WA -
testcase_57 AC 94 ms
15,576 KB
testcase_58 AC 94 ms
15,440 KB
testcase_59 AC 95 ms
15,504 KB
testcase_60 AC 94 ms
15,548 KB
testcase_61 AC 93 ms
15,512 KB
testcase_62 AC 94 ms
15,416 KB
testcase_63 WA -
testcase_64 AC 93 ms
15,340 KB
testcase_65 AC 94 ms
15,468 KB
testcase_66 AC 93 ms
15,440 KB
testcase_67 AC 95 ms
15,344 KB
testcase_68 AC 93 ms
15,384 KB
testcase_69 AC 93 ms
15,392 KB
testcase_70 AC 93 ms
15,392 KB
testcase_71 AC 95 ms
15,412 KB
testcase_72 AC 93 ms
15,316 KB
testcase_73 AC 95 ms
15,364 KB
testcase_74 AC 91 ms
15,392 KB
testcase_75 AC 87 ms
15,344 KB
testcase_76 WA -
testcase_77 AC 90 ms
15,340 KB
testcase_78 AC 89 ms
15,432 KB
testcase_79 AC 89 ms
15,388 KB
testcase_80 WA -
testcase_81 WA -
testcase_82 AC 89 ms
15,448 KB
testcase_83 AC 88 ms
15,392 KB
testcase_84 WA -
testcase_85 AC 89 ms
15,616 KB
testcase_86 WA -
testcase_87 WA -
testcase_88 AC 90 ms
15,388 KB
testcase_89 AC 89 ms
15,532 KB
testcase_90 WA -
testcase_91 AC 95 ms
15,564 KB
testcase_92 AC 92 ms
15,416 KB
testcase_93 AC 91 ms
15,444 KB
testcase_94 AC 92 ms
15,392 KB
testcase_95 AC 92 ms
15,344 KB
testcase_96 WA -
testcase_97 AC 94 ms
15,384 KB
testcase_98 AC 94 ms
15,348 KB
testcase_99 AC 258 ms
15,760 KB
testcase_100 WA -
testcase_101 WA -
testcase_102 AC 92 ms
15,408 KB
testcase_103 AC 94 ms
15,416 KB
testcase_104 AC 91 ms
15,388 KB
testcase_105 AC 92 ms
15,340 KB
testcase_106 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:34: warning: assigned but unused variable - k
Main.rb:45: warning: assigned but unused variable - i
Syntax OK

ソースコード

diff #

require 'prime'

N_MAX = 110
$mp = Array.new(N_MAX).map{Array.new(N_MAX,0)}
$used = Array.new(N_MAX).map{Array.new(N_MAX,false)}
$n = gets.chomp.to_i

$vx = [1,0,-1,0]
$vy = [0,1,0,-1]
def dfs(w,h,y,x)
  return false if y<0 || x<0 || y>=h || x>=w
  return false if $used[y][x]
  $used[y][x] = true
  return false if $mp[y][x]==1e9
  return true if $mp[y][x]==$n
  t = $mp[y][x]
  return false if isP_MR(t)
  for i in 0...4
    return true if dfs(w,h,y+$vy[i],x+$vx[i])
  end
  return false
end

def powmod(a,b,m)
  return 1 if b==0
  p = powmod(a,b/2,m)
  p = p*p%m
  p = p*a%m if b%2==1
  return p
end

# http://d.hatena.ne.jp/pashango_p/20090704/1246692091
def isP_MR(q)
  k = 10000
  # return Prime.prime?(q) if q<10000000
  return true if q==2
  return false if q<2
  return false if q&1==0

  d = (q-1)>>1
  while d&1==0
    d>>=1
  end
  r = [q-1,(2*Math.log(q)**2).floor].min
  for i in 2..r
    a = r
    t = d
    y = powmod(a,t,q)
    while t!=q-1 && y!=1 && y!=q-1
      y = powmod(y,2,q)
      t <<= 1
    end
    if y!=q-1 && t&1==0
      return false
    end
  end
  return true
end

if $n<100
  for w in 3...$n
    h = $n/w + ( $n%w == 0 ? 0 : 1 )
    for i in 0...N_MAX
      for j in 0...N_MAX
        $mp[i][j] = 1e9
        if i<h && j<w && i*w+j<$n
          $mp[i][j] = i*w+j+1
        end
        $used[i][j] = false
      end
    end
    if dfs(w,h,0,0)
      puts w
      break
    end
  end
else
  c = 8
  while true
    # process
    if $n%c!=1
      puts c
      break
    end
    if isP_MR($n-c)
      puts c
      break
    end
    # succeed
    while true
      c += 2
      break if isP_MR(c+1)
    end
  end
end
0