結果

問題 No.12 限定された素数
ユーザー nebukuro09
提出日時 2016-11-11 12:43:23
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 177 ms / 5,000 ms
コード長 1,168 bytes
コンパイル時間 807 ms
コンパイル使用メモリ 114,876 KB
実行使用メモリ 12,564 KB
最終ジャッジ日時 2024-06-12 05:04:08
合計ジャッジ時間 6,274 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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