結果

問題 No.12 限定された素数
ユーザー kk
提出日時 2021-02-19 20:43:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 5,000 ms
コード長 1,063 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 200,776 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:16:11
合計ジャッジ時間 4,412 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
4,348 KB
testcase_01 AC 46 ms
4,352 KB
testcase_02 AC 46 ms
4,348 KB
testcase_03 AC 46 ms
4,352 KB
testcase_04 AC 47 ms
4,352 KB
testcase_05 AC 45 ms
4,352 KB
testcase_06 AC 48 ms
4,356 KB
testcase_07 AC 47 ms
4,348 KB
testcase_08 AC 48 ms
4,352 KB
testcase_09 AC 48 ms
4,348 KB
testcase_10 AC 46 ms
4,352 KB
testcase_11 AC 47 ms
4,352 KB
testcase_12 AC 47 ms
4,352 KB
testcase_13 AC 46 ms
4,352 KB
testcase_14 AC 47 ms
4,348 KB
testcase_15 AC 45 ms
4,352 KB
testcase_16 AC 47 ms
4,352 KB
testcase_17 AC 45 ms
4,352 KB
testcase_18 AC 47 ms
4,348 KB
testcase_19 AC 48 ms
4,356 KB
testcase_20 AC 46 ms
4,348 KB
testcase_21 AC 43 ms
4,348 KB
testcase_22 AC 44 ms
4,356 KB
testcase_23 AC 46 ms
4,352 KB
testcase_24 AC 44 ms
4,352 KB
testcase_25 AC 45 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

vector<bool> psieve(int n) {
  vector<bool> pr(n);
  for (int i = 2; i < n; i++)
    pr[i] = true;
  for (int i = 2; i * i < n; i++) {
    if (!pr[i]) continue;
    for (int j = i * i; j < n; j += i)
      pr[j] = false;
  }
  return pr;
}

int calc(int x) {
  int mask = 0;
  while (x) {
    mask |= 1<<(x % 10);
    x /= 10;
  }
  return mask;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  int valid = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    valid |= 1<<x;
  }
    
  int ret = -1;
  auto pr = psieve(5000000+1);
  for (int i = 1; i <= 5000000; ) {
    int j = i;
    int acc = 0;
    while (j <= 5000000) {
      bool ck = true;
      if (pr[j]) {
        acc |= calc(j);
        if ((valid | acc) != valid)
          ck = false;
      }
      if (!ck)
        break;
      if (acc == valid)
        ret = max(ret, j - i);
      ++j;
    }
    i = j + 1;
  }

  cout << ret << endl;
  
  return 0;
}
0