結果

問題 No.12 限定された素数
ユーザー simansiman
提出日時 2021-12-26 02:03:49
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 5,000 ms
コード長 1,959 bytes
コンパイル時間 4,002 ms
コンパイル使用メモリ 130,804 KB
実行使用メモリ 13,208 KB
最終ジャッジ日時 2023-10-22 06:47:33
合計ジャッジ時間 9,987 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 213 ms
13,208 KB
testcase_01 AC 178 ms
13,208 KB
testcase_02 AC 189 ms
13,208 KB
testcase_03 AC 166 ms
13,208 KB
testcase_04 AC 198 ms
13,208 KB
testcase_05 AC 155 ms
13,208 KB
testcase_06 AC 154 ms
13,208 KB
testcase_07 AC 159 ms
13,208 KB
testcase_08 AC 162 ms
13,208 KB
testcase_09 AC 184 ms
13,208 KB
testcase_10 AC 152 ms
13,208 KB
testcase_11 AC 149 ms
13,208 KB
testcase_12 AC 162 ms
13,208 KB
testcase_13 AC 164 ms
13,208 KB
testcase_14 AC 167 ms
13,208 KB
testcase_15 AC 164 ms
13,208 KB
testcase_16 AC 154 ms
13,208 KB
testcase_17 AC 224 ms
13,208 KB
testcase_18 AC 205 ms
13,208 KB
testcase_19 AC 205 ms
13,208 KB
testcase_20 AC 180 ms
13,208 KB
testcase_21 AC 168 ms
13,208 KB
testcase_22 AC 209 ms
13,208 KB
testcase_23 AC 209 ms
13,208 KB
testcase_24 AC 218 ms
13,208 KB
testcase_25 AC 164 ms
13,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

bool is_prime[5000010];

void setup_prime_table() {
  memset(is_prime, false, sizeof(is_prime));
  bool checked[5000010];
  memset(checked, false, sizeof(checked));

  for (ll i = 2; i <= 5000000; ++i) {
    if (!checked[i]) {
      is_prime[i] = true;
      checked[i] = true;

      for (ll j = i * i; j <= 5000000; j += i) {
        checked[j] = true;
      }
    }
  }
}

bool A[10];

bool is_valid_range(vector<int> &counter) {
  for (int v = 0; v <= 9; ++v) {
    if (A[v] && counter[v] == 0) return false;
    if (not A[v] && counter[v] > 0) return false;
  }

  return true;
}

bool can_move(vector<int> &counter) {
  for (int v = 0; v <= 9; ++v) {
    if (not A[v] && counter[v] > 0) return false;
  }

  return true;
}

void update_counter(ll v, int diff, vector<int> &counter) {
  bool checked[10];
  memset(checked, false, sizeof(checked));

  while (v > 0) {
    int i = v % 10;
    if (not checked[i]) counter[i] += diff;
    checked[i] = true;

    v /= 10;
  }
}

int main() {
  int N;
  cin >> N;
  memset(A, false, sizeof(A));
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A[a] = true;
  }

  setup_prime_table();
  int ans = -1;
  vector<int> counter(10, 0);
  int K = 1;
  int L = 1;
  int lim = 5000000;

  while (K <= L) {
    bool ok = can_move(counter);
    if (is_valid_range(counter)) {
      ans = max(ans, L - K);
    }

    if (L >= lim) {
      if (is_prime[K]) {
        update_counter(K, -1, counter);
      }
      ++K;
    } else if (ok || K >= L) {
      ++L;
      if (is_prime[L]) {
        update_counter(L, 1, counter);
      }
    } else {
      if (is_prime[K]) {
        update_counter(K, -1, counter);
      }
      ++K;
    }
  }

  cout << ans << endl;

  return 0;
}
0