結果

問題 No.12 限定された素数
ユーザー letrangerjpletrangerjp
提出日時 2017-05-15 01:15:28
言語 Ruby
(3.4.1)
結果
AC  
実行時間 1,412 ms / 5,000 ms
コード長 682 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 28,000 KB
最終ジャッジ日時 2024-11-24 09:56:00
合計ジャッジ時間 35,852 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require "prime"

def a2m(a)
  a.inject(0){|r, n| r | 1<<n}
end

def i2m(n)
  r = 0
  while n > 0
    n, m = n.divmod(10)
    r |= 1 << m
  end
  r
end

def s2m(s)
  s.bytes.inject(0){|r,n|r|1<<n-48}
end

def f(a)
  mask = s2m(a.join)

  max = -1
  start = 1
  last_mask = Prime.each(PRIME_MAX).inject(0){|curr_mask, pr|
    pr_mask = i2m(pr)

    if ((curr_mask | pr_mask) & ~mask) != 0
      max = [max, pr - start - 1].max if curr_mask == mask
      start = pr + 1
      0
    else
      curr_mask | pr_mask
    end
  }
  max = [max, PRIME_MAX - start].max if last_mask == mask
  max
end

PRIME_MAX = 5_000_000
N = gets.to_i
A = gets.split.take(N)

p N == 10 ? PRIME_MAX-1 : f(A)
0