結果

問題 No.12 限定された素数
ユーザー atetubouatetubou
提出日時 2014-11-14 23:35:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,336 ms / 5,000 ms
コード長 2,314 bytes
コンパイル時間 1,290 ms
コンパイル使用メモリ 148,880 KB
実行使用メモリ 15,852 KB
最終ジャッジ日時 2023-08-15 22:28:02
合計ジャッジ時間 27,053 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,326 ms
15,516 KB
testcase_01 AC 810 ms
15,708 KB
testcase_02 AC 1,154 ms
15,580 KB
testcase_03 AC 1,332 ms
15,568 KB
testcase_04 AC 1,172 ms
15,712 KB
testcase_05 AC 567 ms
15,716 KB
testcase_06 AC 501 ms
15,520 KB
testcase_07 AC 582 ms
15,852 KB
testcase_08 AC 674 ms
15,520 KB
testcase_09 AC 924 ms
15,564 KB
testcase_10 AC 569 ms
15,712 KB
testcase_11 AC 646 ms
15,704 KB
testcase_12 AC 560 ms
15,720 KB
testcase_13 AC 583 ms
15,572 KB
testcase_14 AC 673 ms
15,576 KB
testcase_15 AC 582 ms
15,656 KB
testcase_16 AC 426 ms
15,520 KB
testcase_17 AC 1,336 ms
15,716 KB
testcase_18 AC 1,312 ms
15,724 KB
testcase_19 AC 1,314 ms
15,644 KB
testcase_20 AC 1,037 ms
15,572 KB
testcase_21 AC 793 ms
15,516 KB
testcase_22 AC 1,317 ms
15,576 KB
testcase_23 AC 1,316 ms
15,568 KB
testcase_24 AC 1,327 ms
15,524 KB
testcase_25 AC 589 ms
15,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef ll li;
typedef pair<ll,ll> PI;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define REP(i, n) rep (i, n)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(),(a).end()
#define FLL(a,b) memset((a),b,sizeof(a))
#define CLR(a) memset((a),0,sizeof(a))
#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)
#define FORR(it,a) for(__typeof(a.rbegin())it=a.rbegin();it!=a.rend();++it)
template<typename T,typename U> ostream& operator<< (ostream& out, const pair<T,U>& val){return out << "(" << val.F << ", " << val.S << ")";}
template<class T> ostream& operator<< (ostream& out, const vector<T>& val){out << "{";rep(i,SZ(val)) out << (i?", ":"") << val[i];return out << "}";}
#define declare(a,it) __typeof(a) it=a
const double EPS = 1e-8;

const int dx[]={1,0,-1,0,1,1,-1,-1,0};
const int dy[]={0,1,0,-1,-1,1,-1,1,0};

const int MAX_P = 5100000;
bool npr[MAX_P];

int main(int argc, char *argv[])
{
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  int appa[10] = {};
  rep(i,n) appa[a[i]] = 1;
  
  npr[0] = npr[1] = 1;
  vector<int> app[10];
  int vis[10] = {};
  for(int i = 2; i < MAX_P; ++i){
    if(npr[i]) continue;
    int t = i;
    if(i<=5000000){
      while(t){
        int mo = t%10;
        if(vis[mo] != i)
          app[mo].pb(i);
        vis[mo] = i;
        t /= 10;
      }
    }
    for(int j = i*2; j < MAX_P; j += i)
      npr[j] = true;
  }

  //cerr << "hoge" << endl;
  
  int ans = -1;
  int lim = 5e6;
  for(int k = 1; k <= lim; ++k){
    int l =  lim;
    bool ok = true;
    rep(i,10){
      if(appa[i]) continue;
      auto it = lower_bound(ALL(app[i]),k);
      if(app[i].end() == it)
        continue;
      l = min(l, *it-1);
    }
    
    rep(i,10){
      if(!appa[i]) continue;
      auto it = upper_bound(ALL(app[i]),k-1);
      if(app[i].end() == it || *it > l){
        ok = false;
        break;
      }
    }
    
    if(ok){
      //cout << k << " " << l << endl;
      ans = max(ans, l - k);
    }
  }

  cout << ans << endl;
  
  return 0;
}
0