結果

問題 No.12 限定された素数
ユーザー totori_nyaa
提出日時 2020-02-12 01:59:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 1,607 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 169,668 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-11-24 10:25:09
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MAX = 5e6+1;
bool p[MAX];
void init(){
    for(int i=0;i<MAX;i++) p[i]=1;
    p[1]=0;
    p[0]=1;
    for(int i=2;i*i<=MAX;i++){
        if(p[i]){
            for(int j=i+i;j<MAX;j+=i){
                p[j] = false;
            }
        }
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
 
    int n;
    cin>>n;
    init();
    int ans = -1;
    int bit = 0;
    for(int i=0;i<n;i++){
        int a; cin>>a;
        bit += (1<<a);
    }
    int r = 1;
    int cnt[10]={};
    for(int i=1;i<MAX;i++){
        if(MAX - i < ans) break;
        if(p[i-1]!=1) continue;
        r=i;
        for(int j=0;j<10;j++) cnt[j]=0;
        bool ok = true;
        while(r<MAX){
            int ret = 0;
            if(p[r]==0){
                r++;
                continue;
            }
            int now = r;
            while(now){
                int t = now%10;
                ret |= (1<<t);
                if((bit & (1<<t)) == false){
                    ok = false;
                    break;
                }
                now /= 10;
            }
            if(!ok) break;
            for(int j=0;j<10;j++){
                if(ret & (1<<j)) cnt[j]++;
            }
            r++;
        }
        bool pos = true;
        for(int j=0;j<10;j++){
            if((cnt[j]>=1) && ((bit & (1<<j))==false)) pos = false;
            if((cnt[j]==0) && (bit & (1<<j))) pos = false;
        }
        if(pos) ans = max(ans,r-i-1);
    }
    cout << ans << endl;
    
}
0