結果

問題 No.12 限定された素数
ユーザー AuroraAurora
提出日時 2022-05-08 12:36:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 1,157 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 169,912 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 07:46:51
合計ジャッジ時間 5,017 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
4,376 KB
testcase_01 AC 79 ms
4,380 KB
testcase_02 AC 69 ms
4,376 KB
testcase_03 AC 92 ms
4,376 KB
testcase_04 AC 70 ms
4,380 KB
testcase_05 AC 85 ms
4,380 KB
testcase_06 AC 89 ms
4,376 KB
testcase_07 AC 90 ms
4,376 KB
testcase_08 AC 82 ms
4,376 KB
testcase_09 AC 78 ms
4,376 KB
testcase_10 AC 84 ms
4,380 KB
testcase_11 AC 92 ms
4,380 KB
testcase_12 AC 90 ms
4,376 KB
testcase_13 AC 86 ms
4,376 KB
testcase_14 AC 82 ms
4,376 KB
testcase_15 AC 87 ms
4,384 KB
testcase_16 AC 91 ms
4,376 KB
testcase_17 AC 63 ms
4,380 KB
testcase_18 AC 63 ms
4,376 KB
testcase_19 AC 63 ms
4,376 KB
testcase_20 AC 73 ms
4,380 KB
testcase_21 AC 79 ms
4,376 KB
testcase_22 AC 63 ms
4,376 KB
testcase_23 AC 64 ms
4,380 KB
testcase_24 AC 64 ms
4,376 KB
testcase_25 AC 85 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

int main(){
  int N;
  cin >> N;
  vector<char> A(N);
  for(int i=0;i<N;i++) cin >> A[i];
  int M = 5000000;
  vector<bool> Prime(M+1,true);
  Prime[0] = Prime[1] = false;
  for(int i=2;i<=M;i++){
    if(Prime[i]){
      for(int j=2*i;j<=M;j+=i){
        Prime[j] = false;
      }
    }
  }
  int ans = -1;
  int L=1,R=1;
  vector<bool> used(N,false);
  bool can = false;
  for(int i=2;i<=M;i++){
    if(!Prime[i]){
      R++;
      if(can) ans = max(ans,R-L);
      
    }
    else{
      string S = to_string(i);
      bool ok = true;
      for(int j=0;j<S.size();j++){
        bool exist = false;
        for(int k=0;k<N;k++){
          if(S[j] == A[k]){
            exist = true;
            used[k] = true;
          }
        }
        if(!exist) ok = false;
      }
      if(ok){
        bool clear = true;
        for(int j=0;j<N;j++) if(!used[j]) clear = false;
        if(clear) can = true;
        R++;
        if(can) ans = max(ans,R-L);
      }
      else{
        can = false;
        for(int j=0;j<N;j++) used[j] = false;
        L = i+1;
        R = i;
      }
    }
  }
  cout << ans << endl;
}
0