結果

問題 No.12 限定された素数
ユーザー momoyuumomoyuu
提出日時 2023-03-15 23:26:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,789 bytes
コンパイル時間 2,856 ms
コンパイル使用メモリ 248,156 KB
実行使用メモリ 22,884 KB
最終ジャッジ日時 2023-10-18 12:37:35
合計ジャッジ時間 6,760 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
22,884 KB
testcase_01 AC 107 ms
22,884 KB
testcase_02 AC 92 ms
22,884 KB
testcase_03 AC 91 ms
22,884 KB
testcase_04 AC 92 ms
22,884 KB
testcase_05 AC 118 ms
22,884 KB
testcase_06 AC 99 ms
22,884 KB
testcase_07 AC 95 ms
22,884 KB
testcase_08 AC 100 ms
22,884 KB
testcase_09 AC 100 ms
22,884 KB
testcase_10 AC 98 ms
22,884 KB
testcase_11 AC 93 ms
22,884 KB
testcase_12 AC 102 ms
22,884 KB
testcase_13 AC 99 ms
22,884 KB
testcase_14 AC 105 ms
22,884 KB
testcase_15 AC 105 ms
22,884 KB
testcase_16 AC 104 ms
22,884 KB
testcase_17 AC 103 ms
22,884 KB
testcase_18 AC 104 ms
22,884 KB
testcase_19 AC 99 ms
22,884 KB
testcase_20 AC 116 ms
22,884 KB
testcase_21 AC 133 ms
22,884 KB
testcase_22 AC 132 ms
22,884 KB
testcase_23 AC 101 ms
22,884 KB
testcase_24 AC 97 ms
22,884 KB
testcase_25 AC 96 ms
22,884 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<int> cnt(10,-1);
    for(int i = 0;i<n;i++) cnt[a[i]]=0;

    int R = 5e6;
    //cout<<R<<endl;
    int L = 1;
    vector<int> p(R+1,-1);
    p[0] = 1;
    p[1] = 1;
    for(int i = 2;i<=R;i++){
        if(p[i]==1) continue;
        assert(p[i]==-1);
        p[i] = 0;
        for(int j = i+i;j<=R;j+=i)p[j]=1;
    }
    int ans = -1;
    int cc = 0;
    int l = 1,r = 1;
    while(l<=R&&r<=R){
        assert(p[r]!=-1);
        if(p[r]){
            r++;
            continue;
        }
        //cout<<r<<endl;
        bool q = true;
        int m = r;
        while(m){
            int now = m % 10;
            m /= 10;
            if(cnt[now]==-1) q = false;
            else cnt[now]++;
        }
        if(q){
            cc++;
            r++;
            continue;
        }
        if(cc==0){
            l = r = r + 1;
            for(int i = 0;i<n;i++) cnt[a[i]] = 0;
            continue;
        }
        assert(cc);
        cc = 0;
        int now = r - 1 - l;
        q = true;
        m = r;
        while(m){
            int now = m % 10;
            m /= 10;
            if(cnt[now]==-1) q = false;
            else cnt[now]--;
        }
        q = true;
        for(int i = 0;i<n;i++){
            if(cnt[a[i]]==0) q = false;
            cnt[a[i]] = 0;
        }
        for(int i = 0;i<10;i++) assert(cnt[i]==0||cnt[i]==-1);
        if(q) ans = max(ans,now);
        //cout<<now<<" "<<l<<" "<<r-1<<endl;
        l = r = r + 1;
    }
    //cout<<ans<<endl;
    bool q = true;
    for(int i = 0;i<n;i++){
        if(cnt[a[i]] == 0) q = false;
    }
    if(q&&cc) ans = max(ans,R-l);
    cout<<ans<<endl;    
}

0