結果

問題 No.12 限定された素数
ユーザー 0w10w1
提出日時 2016-12-04 16:37:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 5,000 ms
コード長 1,858 bytes
コンパイル時間 1,701 ms
コンパイル使用メモリ 175,572 KB
実行使用メモリ 22,844 KB
最終ジャッジ日時 2023-08-15 23:35:52
合計ジャッジ時間 6,323 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
22,472 KB
testcase_01 AC 108 ms
22,508 KB
testcase_02 AC 111 ms
22,560 KB
testcase_03 AC 201 ms
22,460 KB
testcase_04 AC 119 ms
22,472 KB
testcase_05 AC 136 ms
22,476 KB
testcase_06 AC 135 ms
22,560 KB
testcase_07 AC 150 ms
22,680 KB
testcase_08 AC 125 ms
22,572 KB
testcase_09 AC 116 ms
22,512 KB
testcase_10 AC 119 ms
22,508 KB
testcase_11 AC 178 ms
22,756 KB
testcase_12 AC 153 ms
22,412 KB
testcase_13 AC 132 ms
22,468 KB
testcase_14 AC 117 ms
22,720 KB
testcase_15 AC 123 ms
22,516 KB
testcase_16 AC 171 ms
22,520 KB
testcase_17 AC 102 ms
22,844 KB
testcase_18 AC 97 ms
22,564 KB
testcase_19 AC 102 ms
22,408 KB
testcase_20 AC 112 ms
22,560 KB
testcase_21 AC 121 ms
22,524 KB
testcase_22 AC 106 ms
22,632 KB
testcase_23 AC 106 ms
22,464 KB
testcase_24 AC 123 ms
22,700 KB
testcase_25 AC 131 ms
22,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef vector< pii > vp;
typedef vector< vp > vvp;
typedef vector< string > vs;
typedef vector< double > vd;
typedef vector< vd > vvd;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

int N;
vi A;

void init(){
  cin >> N;
  A = vi( N );
  for( int i = 0; i < N; ++i )
    cin >> A[ i ];
}

vi np;
vi ng;
vi ok;

void preprocess(){
  sort( A.begin(), A.end() );
  ng = vi( 10, 1 );
  for( int i = 0; i < A.size(); ++i )
    ng[ A[ i ] ] = 0;
  ok = vi( 10 );
  for( int i = 0; i < A.size(); ++i )
    ok[ A[ i ] ] = 1;
  np = vi( 5000001 + 1 );
  np[ 1 ] = 1;
  for( int i = 2; i < 5000001; ++i )
    if( not np[ i ] )
      for( int j = i + i; j < 5000001; j += i )
        np[ j ] = 1;
}

void solve(){
  int ans = -1;
  for( int i = 1; i <= 5000001; ++i ){
    set< int > dig;
    for( int j = i; j <= 5000001; ++j ){
      if( np[ j ] ) continue;
      int bad_flag = 0;
      for( int u = j; u; u /= 10 )
        if( ng[ u % 10 ] ){
          bad_flag = 1;
          break;
        }
      if( bad_flag or j == 5000001 ){
        int bad_bad_flag = 0;
        for( int k = 0; k < A.size(); ++k )
          if( not dig.count( A[ k ] ) )
            bad_bad_flag = 1;
        if( not bad_bad_flag )
          upmax( ans, j - 1 - i );
        i = j;
        break;
      }
      for( int u = j; u; u /= 10 )
        dig.emplace( u % 10 );
    }
  }
  cout << ans << endl;
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0