結果

問題 No.12 限定された素数
ユーザー letrangerjpletrangerjp
提出日時 2017-05-15 00:46:08
言語 Ruby
(3.3.0)
結果
AC  
実行時間 2,015 ms / 5,000 ms
コード長 572 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 11,372 KB
実行使用メモリ 27,528 KB
最終ジャッジ日時 2023-08-16 00:19:06
合計ジャッジ時間 53,640 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,996 ms
27,504 KB
testcase_01 AC 2,015 ms
27,380 KB
testcase_02 AC 1,988 ms
27,528 KB
testcase_03 AC 77 ms
15,216 KB
testcase_04 AC 1,985 ms
27,448 KB
testcase_05 AC 1,991 ms
27,412 KB
testcase_06 AC 1,920 ms
27,180 KB
testcase_07 AC 1,921 ms
27,416 KB
testcase_08 AC 1,920 ms
27,292 KB
testcase_09 AC 1,923 ms
27,124 KB
testcase_10 AC 1,953 ms
27,256 KB
testcase_11 AC 1,936 ms
27,348 KB
testcase_12 AC 1,921 ms
27,448 KB
testcase_13 AC 1,924 ms
27,312 KB
testcase_14 AC 1,924 ms
27,488 KB
testcase_15 AC 1,995 ms
27,296 KB
testcase_16 AC 1,937 ms
27,228 KB
testcase_17 AC 1,928 ms
27,360 KB
testcase_18 AC 1,917 ms
27,328 KB
testcase_19 AC 1,915 ms
27,400 KB
testcase_20 AC 1,924 ms
27,432 KB
testcase_21 AC 1,931 ms
27,500 KB
testcase_22 AC 1,923 ms
27,516 KB
testcase_23 AC 1,931 ms
27,236 KB
testcase_24 AC 1,942 ms
27,272 KB
testcase_25 AC 1,951 ms
27,344 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require "prime"

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

def f(a)
  mask = a2m(a)

  max = -1
  start = 1
  last_mask = Prime.each(PRIME_MAX).inject(0){|curr_mask, pr|
    pr_mask = a2m(pr.to_s.chars.map(&:to_i))

    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).map(&:to_i)

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