結果

問題 No.12 限定された素数
ユーザー ChanyuhChanyuh
提出日時 2019-10-03 01:30:04
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,324 bytes
コンパイル時間 1,235 ms
コンパイル使用メモリ 105,940 KB
実行使用メモリ 814,580 KB
最終ジャッジ日時 2024-04-14 09:02:13
合計ジャッジ時間 4,386 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

vector<int> prime_list(int l,int r){//[l,r]に含まれる素数表
    vector<int> li(r+1,1);
    for(int i=2;i<=r;i++){
        if (!li[i]) continue;
        for(int j=2*i;j<=r;j+=i){
            li[j]=0;
        }
    }
    vector<int> res={};
    for(int i=max(2,l);i<=r;i++){
        if (li[i]) res.push_back(i);
    }
    return res;
}

int n,x;
int a[11],dp[500000][1024];

int check(int x){
    int res=0;
    while(x>0){
        int a=x%10;
        res|=(1 << a);
        x/=10;
    }
    return res;
}

void solve(){
    cin >> n;
    if (n==10){
        cout << 4999999 << endl;
        return;
    }
    rep(i,n){
        cin >> a[i];
        x|=(1 << a[i]);
    }
    vector<int> prime=prime_list(0,5000000);
    int m=prime.size();
    rep(i,m+1){
        //cout << prime[i] << endl;
        rep(j,1024){
            dp[i][j]=-1;
        }
    }
    cout << m << endl;
    dp[0][0]=0;
    int ans=-1;
    rep(i,m){
        int s=check(prime[i]);
        if (i!=0) dp[i+1][s]=prime[i+1]-prime[i-1]-2;
        else dp[2][s]=2;
        rep(j,1024){
            if (dp[i][j]!=-1)  dp[i+1][j|s]=max(dp[i][j]+prime[i+1]-prime[i],dp[i+1][j]);
        }
        //cout << ans << endl;
        ans=max(ans,dp[i+1][x]);
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0