結果

問題 No.12 限定された素数
ユーザー te-shte-sh
提出日時 2017-01-13 18:10:13
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 1,637 bytes
コンパイル時間 798 ms
コンパイル使用メモリ 118,792 KB
実行使用メモリ 6,660 KB
最終ジャッジ日時 2023-09-03 00:37:26
合計ジャッジ時間 3,633 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
6,432 KB
testcase_01 AC 51 ms
6,380 KB
testcase_02 AC 50 ms
6,404 KB
testcase_03 AC 58 ms
6,432 KB
testcase_04 AC 50 ms
6,444 KB
testcase_05 AC 51 ms
6,384 KB
testcase_06 AC 53 ms
6,432 KB
testcase_07 AC 55 ms
6,384 KB
testcase_08 AC 51 ms
6,440 KB
testcase_09 AC 50 ms
6,400 KB
testcase_10 AC 50 ms
6,380 KB
testcase_11 AC 56 ms
6,400 KB
testcase_12 AC 54 ms
6,660 KB
testcase_13 AC 52 ms
6,432 KB
testcase_14 AC 52 ms
6,484 KB
testcase_15 AC 53 ms
6,488 KB
testcase_16 AC 57 ms
6,428 KB
testcase_17 AC 50 ms
6,384 KB
testcase_18 AC 50 ms
6,384 KB
testcase_19 AC 50 ms
6,400 KB
testcase_20 AC 50 ms
6,424 KB
testcase_21 AC 50 ms
6,432 KB
testcase_22 AC 50 ms
6,384 KB
testcase_23 AC 50 ms
6,444 KB
testcase_24 AC 50 ms
6,396 KB
testcase_25 AC 51 ms
6,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.typecons;  // Tuple, Nullable, BigFlags
import std.math;      // math functions
import std.numeric;   // gcd
import std.bigint;    // BigInt
import std.random;    // random
import std.bitmanip;  // BitArray
import core.bitop;    // bit operation
import std.regex;     // RegEx
import std.uni;       // unicode

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.to!(int[]);
  writeln(calc(ai));
}

int calc(int[] ai)
{
  auto ma = 5_000_000;

  auto pi = primes(ma);
  auto s = ai.map!toBitNum.fold!"a | b";
  auto t = ~s & ((1 << 10) - 1);

  auto r = -1, i = 0;
  while (i < pi.length) {
    while (toBitNum(pi[i]) & t) {
      if (++i >= pi.length) return r;
    }

    auto j = i;
    while (!(toBitNum(pi[j]) & t)) {
      if (j++ >= pi.length - 1) break;
    }

    if (pi[i..j].map!toBitNum.fold!"a | b" == s) {
      auto p1 = i == 0 ? 1 : pi[i - 1] + 1;
      auto p2 = j == pi.length ? ma : pi[j] - 1;
      r = max(r, p2 - p1);
    }

    i = j + 1;
  }

  return r;
}

int toBitNum(int a)
{
  if (a == 0) return 1;

  int r = 0;
  for (; a != 0; a /= 10)
    r |= (1 << a % 10);
  return r;
}

int[] primes(int n)
{
  auto sieve = new bool[]((n + 1) / 2);
  sieve[] = true;

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

  auto r = sieve.enumerate
    .filter!("a[1]")
    .map!("a[0]").map!("a * 2 + 1")
    .map!(to!int).array;
  r[0] = 2;

  return r;
}
0