結果

問題 No.12 限定された素数
ユーザー raven7959raven7959
提出日時 2022-08-20 09:31:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 5,000 ms
コード長 8,171 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 211,620 KB
実行使用メモリ 31,848 KB
最終ジャッジ日時 2024-04-17 09:06:29
合計ジャッジ時間 7,272 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
31,588 KB
testcase_01 AC 147 ms
31,616 KB
testcase_02 AC 148 ms
31,720 KB
testcase_03 AC 211 ms
31,796 KB
testcase_04 AC 134 ms
31,648 KB
testcase_05 AC 145 ms
31,716 KB
testcase_06 AC 141 ms
31,716 KB
testcase_07 AC 138 ms
31,752 KB
testcase_08 AC 141 ms
31,632 KB
testcase_09 AC 146 ms
31,588 KB
testcase_10 AC 143 ms
31,696 KB
testcase_11 AC 159 ms
31,848 KB
testcase_12 AC 147 ms
31,556 KB
testcase_13 AC 140 ms
31,592 KB
testcase_14 AC 141 ms
31,696 KB
testcase_15 AC 131 ms
31,800 KB
testcase_16 AC 129 ms
31,584 KB
testcase_17 AC 135 ms
31,624 KB
testcase_18 AC 136 ms
31,664 KB
testcase_19 AC 136 ms
31,772 KB
testcase_20 AC 134 ms
31,636 KB
testcase_21 AC 136 ms
31,712 KB
testcase_22 AC 148 ms
31,784 KB
testcase_23 AC 137 ms
31,716 KB
testcase_24 AC 132 ms
31,716 KB
testcase_25 AC 134 ms
31,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n"
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n"
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vld = vector<long double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<long double>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
const int INF = 1e9;
const ll LINF = 2e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
bool ispow2(int i) { return i && (i & -i) == i; }
bool ispow2(ll i) { return i && (i & -i) == i; }
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

static uint32_t RandXor(){
    static uint32_t x=123456789;
    static uint32_t y=362436069;
    static uint32_t z=521288629;
    static uint32_t w=88675123;
    uint32_t t;
 
    t=x^(x<<11);
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

static double Rand01(){
    return (RandXor()+0.5)*(1.0/UINT_MAX);
}

// sieve SE(N): 1からNまでのmin_factor tableを作成
// vector<pii> factorize(int n): nを素因数分解した結果を(p,e)のvectorで返す
// vi divisors(int n): nの正の約数を返す, 約数の個数をdとしてO(d)
// int number_of_divisors(int n): nの正の約数の個数を返す, まあ一応書いただけ

struct sieve{
    vector<bool> is_prime;
    vector<int> min_factor,primes;

    sieve(int N): is_prime(N+1,true),min_factor(N+1,-1){
        is_prime[1]=false;
        min_factor[1]=1;
        for(int i=2;i<=N;i++){
            if(!is_prime[i]) continue;
            primes.push_back(i);
            min_factor[i]=i;
            for(int j=i;j<=N;j+=i){
                if(j!=i) is_prime[j]=false;
                if(min_factor[j]==-1) min_factor[j]=i;
            }
        }
    }

    vector<pair<int,int>> factorize(int n){
        vector<pair<int,int>> ret;
        while(n>1){
            int p=min_factor[n];
            int e=0;
            while(n%p==0){
                e++;
                n/=p;
            }
            ret.emplace_back(p,e);
        }
        return ret;
    }

    vector<int> divisors(int n){
        vector<int> ret={1};
        for(auto [p,e]:factorize(n)){
            int cnt=int(ret.size());
            for(int i=0;i<cnt*e;i++) ret.emplace_back(ret[i]*p);
        }
        sort(ret.begin(),ret.end());
        return ret;
    }

    int number_of_divisors(int n){
        int ret=1;
        for(auto [p,e]:factorize(n)){
            ret*=e+1;
        }
        return ret;
    }
};

// segtree<S,op,e> seg(int n)またはsegtree<S,op,e> seg(vector<S> vec)で初期化
// Sは型, Sを返す関数op(S a,S b)と単位元S e()を設定する
// set(int p,S x): a[p]にxを代入する
// get(int p): a[p]を取得する
// prod(int l,int r): [l,r)をfoldした結果を返す
// all_prod(): [0,n)をfoldした結果を返す
// max_right<f>(int l): bool f(S x)を渡すとl以降でf(prod(l,r))=trueとなる最大のrを返す
// min_left: max_rightの逆

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template <class S, S (*op)(S, S), S (*e)()>
struct segtree{
public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(vector<S>(n, e())) {}
    segtree(const vector<S> &v) : _n(int(v.size())){
        log = ceil_pow2(_n);
        size = 1 << log;
        d = vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) update(i);
    }

    void set(int p, S x){
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++)
            update(p >> i);
    }

    S get(int p){
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r){
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r){
            if (l & 1)
                sml = op(sml, d[l++]);
            if (r & 1)
                smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)>
    int max_right(int l){
        return max_right(l, [](S x)
                         { return f(x); });
    }
    template <class F>
    int max_right(int l, F f){
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do{
            while (l % 2 == 0)
                l >>= 1;
            if (!f(op(sm, d[l]))){
                while (l < size){
                    l = (2 * l);
                    if (f(op(sm, d[l]))){
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)>
    int min_left(int r){
        return min_left(r, [](S x)
                        { return f(x); });
    }
    template <class F>
    int min_left(int r, F f){
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do{
            r--;
            while (r > 1 && (r % 2))
                r >>= 1;
            if (!f(op(d[r], sm))){
                while (r < size){
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))){
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

int op(int l,int r){return l|r;}
int e(){return 0;}

void solve(){
    int N;
    cin>>N;
    int X=0;
    rep(_,N){
        int n;
        cin>>n;
        X|=1<<n;
    }
    int ans=-1;
    sieve ES(5000000);
    vi P={0};
    for(auto p:ES.primes) P.emplace_back(p);
    P.emplace_back(5000001);
    vi V=P;
    for(int i=1;i<sz(V)-1;i++){
        int now=V[i],sm=0;
        while(now){
            sm|=1<<(now%10);
            now/=10;
        }
        V[i]=sm;
    }
    V.back()=2047;
    segtree<int,op,e> seg(V);
    for(int i=1;i<sz(P)-1;i++){
        auto f=[&](int x){
            return (x|X)<=X;
        };
        int idx=seg.max_right(i,f);
        if(seg.prod(i,idx)==X) chmax(ans,P[idx]-P[i-1]-2);
    }
    cout<<ans<<endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();
}
0