結果

問題 No.12 限定された素数
ユーザー imulanimulan
提出日時 2016-07-05 18:16:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 1,466 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 168,320 KB
実行使用メモリ 10,052 KB
最終ジャッジ日時 2023-08-15 23:30:27
合計ジャッジ時間 4,310 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
9,788 KB
testcase_01 AC 61 ms
9,864 KB
testcase_02 AC 60 ms
9,852 KB
testcase_03 AC 58 ms
9,988 KB
testcase_04 AC 61 ms
9,744 KB
testcase_05 AC 61 ms
9,940 KB
testcase_06 AC 60 ms
9,972 KB
testcase_07 AC 60 ms
9,980 KB
testcase_08 AC 61 ms
9,740 KB
testcase_09 AC 61 ms
9,736 KB
testcase_10 AC 62 ms
10,052 KB
testcase_11 AC 60 ms
9,728 KB
testcase_12 AC 61 ms
9,868 KB
testcase_13 AC 61 ms
9,836 KB
testcase_14 AC 61 ms
9,984 KB
testcase_15 AC 60 ms
9,980 KB
testcase_16 AC 61 ms
10,000 KB
testcase_17 AC 62 ms
9,872 KB
testcase_18 AC 60 ms
9,872 KB
testcase_19 AC 61 ms
9,800 KB
testcase_20 AC 62 ms
9,992 KB
testcase_21 AC 61 ms
9,928 KB
testcase_22 AC 62 ms
9,852 KB
testcase_23 AC 60 ms
9,792 KB
testcase_24 AC 60 ms
9,928 KB
testcase_25 AC 60 ms
9,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N=5000000;

bool p[N+1];
vector<int> prime;

int a[10]={0};
int ct[10]={0};

int check()
{
    int ret=0;

    rep(i,10)
    {
        if(a[i]==1)
        {
            if(ct[i]==0) ret=1;
        }
        else
        {
            if(ct[i]>0) return -1;
        }
    }

    return ret;
}

int main()
{
    fill(p,p+N+1,true);
    p[0]=p[1]=false;
    for(int i=2; i<=N; ++i)
    {
        if(p[i]) for(int j=2; i*j<=N; ++j) p[i*j]=false;
    }

    rep(i,N+1) if(p[i]) prime.pb(i);

    int n;
    cin >>n;
    rep(i,n)
    {
        int x;
        cin >>x;
        a[x]=1;
    }

    int ans=-1;
    int P=prime.size();
    int K=1;

    rep(i,P)
    {
        //prime[i]を含む時
        int t=prime[i];
        while(t>0)
        {
            ++ct[t%10];
            t/=10;
        }

        //0:perfect, -1:over, 1:lack
        int state=check();

        if(state==0)
        {
            if(i==P-1) ans=max(ans,N-K);
            else ans=max(ans,prime[i+1]-1-K);
        }
        else if(state==-1)
        {
            rep(j,10) ct[j]=0;
            K=prime[i]+1;
        }
    }

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