結果

問題 No.12 限定された素数
ユーザー latte0119latte0119
提出日時 2016-02-25 17:59:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 247 ms / 5,000 ms
コード長 1,571 bytes
コンパイル時間 1,381 ms
コンパイル使用メモリ 149,568 KB
実行使用メモリ 13,616 KB
最終ジャッジ日時 2023-08-15 23:21:12
合計ジャッジ時間 9,145 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
13,356 KB
testcase_01 AC 243 ms
13,364 KB
testcase_02 AC 242 ms
13,428 KB
testcase_03 AC 240 ms
13,512 KB
testcase_04 AC 244 ms
13,592 KB
testcase_05 AC 245 ms
13,308 KB
testcase_06 AC 243 ms
13,364 KB
testcase_07 AC 247 ms
13,340 KB
testcase_08 AC 244 ms
13,344 KB
testcase_09 AC 242 ms
13,312 KB
testcase_10 AC 243 ms
13,308 KB
testcase_11 AC 243 ms
13,500 KB
testcase_12 AC 244 ms
13,292 KB
testcase_13 AC 243 ms
13,364 KB
testcase_14 AC 243 ms
13,440 KB
testcase_15 AC 245 ms
13,456 KB
testcase_16 AC 246 ms
13,504 KB
testcase_17 AC 244 ms
13,616 KB
testcase_18 AC 244 ms
13,488 KB
testcase_19 AC 243 ms
13,496 KB
testcase_20 AC 244 ms
13,296 KB
testcase_21 AC 244 ms
13,428 KB
testcase_22 AC 243 ms
13,424 KB
testcase_23 AC 243 ms
13,592 KB
testcase_24 AC 243 ms
13,304 KB
testcase_25 AC 244 ms
13,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//#define int long long

typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ln <<endl
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

vint p;
bool f[5000000];

bool ex[400000][10];
int N;
int ok[10],cnt[10];

bool check(){
    rep(i,10)if(!ok[i]&&cnt[i])return false;
    return true;
}

signed main(){
    fill_n(f,5000000,1);
    f[0]=f[1]=0;
    p.pb(0);
    for(int i=2;i<5000000;i++){
        if(!f[i])continue;
        p.pb(i);
        for(int j=2;i*j<5000000;j++)f[i*j]=0;
    }
    p.pb(5000001);

    reps(i,1,p.size()-1){
        string s;
        stringstream ss;ss<<p[i];ss>>s;
        rep(j,s.size())ex[i][s[j]-'0']=1;
    }


    cin>>N;
    rep(i,N){
        int a;cin>>a;ok[a]=1;
    }
    int l=1;
    int ans=-1;
    reps(i,1,p.size()-1){
        rep(j,10)cnt[j]+=ex[i][j];
        while(l<=i&&!check()){
            rep(j,10)cnt[j]-=ex[l][j];
            l++;
        }
        bool y=true;
        rep(j,10)y&=!((cnt[j]!=0)^ok[j]);
        if(y){
            int hoge=p[l-1]+1;
            int piyo=p[i+1]-1;
            chmax(ans,piyo-hoge);
        }
    }

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