結果

問題 No.12 限定された素数
ユーザー nebukuro09nebukuro09
提出日時 2016-11-11 12:43:23
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 196 ms / 5,000 ms
コード長 1,168 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 107,128 KB
実行使用メモリ 13,332 KB
最終ジャッジ日時 2023-09-02 22:53:11
合計ジャッジ時間 6,901 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
11,752 KB
testcase_01 AC 187 ms
12,056 KB
testcase_02 AC 180 ms
11,580 KB
testcase_03 AC 175 ms
11,528 KB
testcase_04 AC 181 ms
11,780 KB
testcase_05 AC 186 ms
12,300 KB
testcase_06 AC 183 ms
11,776 KB
testcase_07 AC 185 ms
12,084 KB
testcase_08 AC 181 ms
11,788 KB
testcase_09 AC 182 ms
12,072 KB
testcase_10 AC 183 ms
12,088 KB
testcase_11 AC 185 ms
12,072 KB
testcase_12 AC 189 ms
12,128 KB
testcase_13 AC 188 ms
11,816 KB
testcase_14 AC 188 ms
12,024 KB
testcase_15 AC 188 ms
11,596 KB
testcase_16 AC 196 ms
11,512 KB
testcase_17 AC 178 ms
11,788 KB
testcase_18 AC 179 ms
12,604 KB
testcase_19 AC 179 ms
13,332 KB
testcase_20 AC 178 ms
11,816 KB
testcase_21 AC 181 ms
12,040 KB
testcase_22 AC 177 ms
12,576 KB
testcase_23 AC 184 ms
12,308 KB
testcase_24 AC 178 ms
12,564 KB
testcase_25 AC 181 ms
12,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main() {
  int N = readln.chomp.to!int;
  bool[10] A;
  foreach (i; readln.split.map!(to!int))
    A[i] = true;
  
  int MAX = 5000000;
  auto pt = new bool[](MAX+1);
  foreach (i; 2..MAX+1) 
    pt[i] = true;
  foreach (i; 2..MAX+1)
    if (pt[i])
      foreach (j; iota(i+i, MAX+1, i))
        pt[j] = false;
  int[] primes;
  foreach (i; 2..MAX+1)
    if (pt[i])
      primes ~= i;
  primes = 0 ~ primes ~ [MAX+1];

  bool[10] count;
  
  int left = 1;
  int right = 1;
  int ans = -1;
  while (right < primes.length-1) {
    foreach (c; primes[right].to!string)
      count[c.to!int-48] = true;
    if (all(iota(10).map!(i => A[i]==count[i]))) {
      ans = max(ans, (primes[right+1]-1)-(primes[left-1]+1));
      right += 1;
    }
    else if (all(iota(10).map!(i => !(!A[i] && count[i])))) {
      right += 1;
    }
    else  {
      right += 1;
      left = right;
      foreach (i; 0..10)
        count[i] = false;
    }
  }
  writeln(ans);
}
0