結果

問題 No.12 限定された素数
ユーザー totori_nyaatotori_nyaa
提出日時 2020-02-12 01:59:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 1,607 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 165,280 KB
実行使用メモリ 8,436 KB
最終ジャッジ日時 2023-08-16 01:11:52
合計ジャッジ時間 4,469 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
8,252 KB
testcase_01 AC 41 ms
8,316 KB
testcase_02 AC 39 ms
8,368 KB
testcase_03 AC 44 ms
8,160 KB
testcase_04 AC 38 ms
8,304 KB
testcase_05 AC 41 ms
8,320 KB
testcase_06 AC 57 ms
8,180 KB
testcase_07 AC 65 ms
8,180 KB
testcase_08 AC 41 ms
8,304 KB
testcase_09 AC 40 ms
8,372 KB
testcase_10 AC 41 ms
8,380 KB
testcase_11 AC 113 ms
8,436 KB
testcase_12 AC 68 ms
8,272 KB
testcase_13 AC 50 ms
8,308 KB
testcase_14 AC 41 ms
8,248 KB
testcase_15 AC 50 ms
8,372 KB
testcase_16 AC 68 ms
8,224 KB
testcase_17 AC 39 ms
8,432 KB
testcase_18 AC 39 ms
8,280 KB
testcase_19 AC 39 ms
8,288 KB
testcase_20 AC 42 ms
8,268 KB
testcase_21 AC 44 ms
8,220 KB
testcase_22 AC 40 ms
8,328 KB
testcase_23 AC 39 ms
8,264 KB
testcase_24 AC 39 ms
8,368 KB
testcase_25 AC 44 ms
8,180 KB
権限があれば一括ダウンロードができます

ソースコード

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