結果

問題 No.12 限定された素数
ユーザー SSRSSSRS
提出日時 2020-11-08 22:12:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 618 ms / 5,000 ms
コード長 1,393 bytes
コンパイル時間 2,812 ms
コンパイル使用メモリ 173,932 KB
実行使用メモリ 137,296 KB
最終ジャッジ日時 2023-09-29 21:44:19
合計ジャッジ時間 19,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 614 ms
137,092 KB
testcase_01 AC 601 ms
136,908 KB
testcase_02 AC 591 ms
136,904 KB
testcase_03 AC 424 ms
137,028 KB
testcase_04 AC 592 ms
137,148 KB
testcase_05 AC 608 ms
137,192 KB
testcase_06 AC 609 ms
137,220 KB
testcase_07 AC 608 ms
136,908 KB
testcase_08 AC 617 ms
137,256 KB
testcase_09 AC 598 ms
136,900 KB
testcase_10 AC 616 ms
137,064 KB
testcase_11 AC 618 ms
137,068 KB
testcase_12 AC 606 ms
137,044 KB
testcase_13 AC 604 ms
137,044 KB
testcase_14 AC 606 ms
136,948 KB
testcase_15 AC 605 ms
137,036 KB
testcase_16 AC 610 ms
137,188 KB
testcase_17 AC 596 ms
137,104 KB
testcase_18 AC 591 ms
137,104 KB
testcase_19 AC 591 ms
136,948 KB
testcase_20 AC 593 ms
136,900 KB
testcase_21 AC 603 ms
137,296 KB
testcase_22 AC 594 ms
137,152 KB
testcase_23 AC 602 ms
137,084 KB
testcase_24 AC 593 ms
136,964 KB
testcase_25 AC 613 ms
137,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<bool> prime(5000001, true);
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= 5000000; i++){
    if (prime[i]){
      for (int j = i * 2; j <= 5000000; j += i){
        prime[j] = false;
      }
    }
  }
  vector<vector<int>> S(5000000);
  for (int i = 1; i <= 5000001; i++){
    if (prime[i]){
      string s = to_string(i);
      for (char c : s){
        S[i - 1].push_back(c - '0');
      }
    }
  }
  vector<int> T(10, 0);
  for (int i = 0; i < N; i++){
    T[A[i]] = 1;
  }
  int ans = -1;
  int R = 0;
  vector<int> cnt(10, 0);
  for (int L = 0; L < 5000000; L++){
    while (R < 5000000){
      vector<int> cnt2 = cnt;
      for (int x : S[R]){
        cnt2[x]++;
      }
      bool ok = true;
      for (int i = 0; i < 10; i++){
        if (T[i] == 0 && cnt2[i] > 0){
          ok = false;
        }
      }
      if (ok){
        swap(cnt, cnt2);
        R++;
      } else {
        break;
      }
    }
    bool ok = true;
    for (int i = 0; i < 10; i++){
      if (T[i] == 1 && cnt[i] == 0){
        ok = false;
      }
    }
    if (ok){
      ans = max(ans, R - L - 1);
    }
    if (R == L){
      R++;
    } else {
      for (int x : S[L]){
        cnt[x]--;
      }
    }
  }
  cout << ans << endl;
}
0