結果

問題 No.12 限定された素数
ユーザー te-shte-sh
提出日時 2016-09-01 12:13:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 1,431 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 113,716 KB
実行使用メモリ 9,516 KB
最終ジャッジ日時 2023-09-02 21:50:05
合計ジャッジ時間 4,282 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
8,712 KB
testcase_01 AC 80 ms
8,768 KB
testcase_02 AC 79 ms
8,764 KB
testcase_03 AC 80 ms
8,764 KB
testcase_04 AC 80 ms
8,708 KB
testcase_05 AC 81 ms
8,956 KB
testcase_06 AC 82 ms
8,696 KB
testcase_07 AC 83 ms
8,968 KB
testcase_08 AC 82 ms
8,756 KB
testcase_09 AC 81 ms
8,924 KB
testcase_10 AC 81 ms
8,776 KB
testcase_11 AC 87 ms
8,744 KB
testcase_12 AC 84 ms
9,516 KB
testcase_13 AC 82 ms
8,836 KB
testcase_14 AC 81 ms
8,716 KB
testcase_15 AC 82 ms
8,780 KB
testcase_16 AC 87 ms
8,708 KB
testcase_17 AC 80 ms
8,716 KB
testcase_18 AC 81 ms
8,776 KB
testcase_19 AC 80 ms
9,204 KB
testcase_20 AC 81 ms
8,756 KB
testcase_21 AC 80 ms
9,028 KB
testcase_22 AC 79 ms
8,728 KB
testcase_23 AC 81 ms
8,712 KB
testcase_24 AC 81 ms
9,516 KB
testcase_25 AC 82 ms
8,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random;
import std.string, std.conv, std.stdio, std.typecons;

const auto maxPeriod = 5000000;

void main()
{
  auto n = readln.chomp.to!int;
  auto ai = readln.split.map!(to!int);

  auto b = 0;
  foreach (a; ai) b |= (1 << a);

  auto pi = primes(maxPeriod);
  auto qi = pi.map!(digits);

  auto maxLen = 0;
  auto s = -1;
  foreach (t, p; pi) {
    if ((qi[t] & ~b) != 0) {
      if (s != -1 && qi[s..t].reduce!("a | b") == b) {
        auto p1 = s == 0 ? 1 : pi[s - 1] + 1;
        auto p2 = t == pi.length - 1 ? maxPeriod : pi[t] - 1;
        auto len = p2 - p1;
        maxLen = max(maxLen, len);
      }
      s = -1;
    } else {
      if (s == -1)
        s = t.to!int;
    }
  }
  if (s != -1) {
    auto t = pi.length;
    auto p1 = s == 0 ? 1 : pi[s - 1] + 1;
    auto p2 = maxPeriod;
    auto len = p2 - p1;
    maxLen = max(maxLen, len);
  }

  writeln(maxLen == 0 ? -1 : maxLen);
}

int digits(int n)
{
  auto r = 0;
  for (; n > 0; n /= 10) r |= (1 << (n % 10));
  return r;
}

int[] primes(int n)
{
  auto sieve = new bool[](n + 1);
  sieve[0..2] = false;
  sieve[2..$] = true;

  foreach (p; 2..n.to!real.sqrt.to!int + 1)
    if (sieve[p])
      foreach (q; iota(p * 2, n + 1, p))
        sieve[q] = false;

  return sieve.enumerate
    .filter!("a[1]")
    .map!("a[0]").map!(to!int).array;
}
0