結果

問題 No.12 限定された素数
ユーザー T1610T1610
提出日時 2019-03-17 21:10:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 5,000 ms
コード長 2,696 bytes
コンパイル時間 2,504 ms
コンパイル使用メモリ 169,024 KB
実行使用メモリ 24,664 KB
最終ジャッジ日時 2023-08-16 00:58:57
合計ジャッジ時間 7,166 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
24,396 KB
testcase_01 AC 142 ms
24,656 KB
testcase_02 AC 119 ms
24,444 KB
testcase_03 AC 175 ms
24,636 KB
testcase_04 AC 133 ms
24,632 KB
testcase_05 AC 145 ms
24,636 KB
testcase_06 AC 138 ms
24,500 KB
testcase_07 AC 146 ms
24,576 KB
testcase_08 AC 124 ms
24,664 KB
testcase_09 AC 133 ms
24,500 KB
testcase_10 AC 121 ms
24,560 KB
testcase_11 AC 147 ms
24,484 KB
testcase_12 AC 174 ms
24,396 KB
testcase_13 AC 166 ms
24,444 KB
testcase_14 AC 159 ms
24,636 KB
testcase_15 AC 163 ms
24,632 KB
testcase_16 AC 191 ms
24,632 KB
testcase_17 AC 115 ms
24,352 KB
testcase_18 AC 120 ms
24,568 KB
testcase_19 AC 121 ms
24,480 KB
testcase_20 AC 119 ms
24,612 KB
testcase_21 AC 131 ms
24,368 KB
testcase_22 AC 118 ms
24,364 KB
testcase_23 AC 114 ms
24,456 KB
testcase_24 AC 115 ms
24,444 KB
testcase_25 AC 121 ms
24,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) ;
#define LINE     ;
#define dumpV(v);
#define STOP     ;
#endif
#define mp make_pair

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


/*
  エラトステネスの篩
  O( n log log n )
  verified yukicoder No. 6
 */
const int MAX_N = 5e6+10;
vector<int> primes;
vector<int> isPrime(MAX_N, 1);
void buildPrimes() {
    isPrime[0] = isPrime[1] = 0;
    REP(i, 2, MAX_N) {
        if(isPrime[i]) {
            primes.push_back(i);
            for(int j = i+i; j < MAX_N; j += i) isPrime[j] = false;
        }
    }
}


int main(){
    buildPrimes();
    int n;
    cin >> n;
    vector<int> v(10);
    rep(i, n) {
        int a;
        cin >> a;
        v[a]++;
    }
    int l = 1, r = 3, ans = -1;
    vector<int> cnt(10, 0);
    cnt[2] = 1;
    constexpr int MAX = 5e6;
    auto check = [&]() {
        rep(i, 10) {
            if(v[i] > 0 && cnt[i] == 0) return 1;
            if(v[i] == 0 && cnt[i] > 0) return -1;
        }
        return 0;
    };
    auto update = [&](int n, bool plus) {
        int x = n;
        while(x > 0) {
            if(plus) cnt[x%10]++;
            else cnt[x%10]--;
            x /= 10;
        }
    };
    while(r <= MAX) {
        // dump(mp(l, r));
        // dumpV(cnt);
        if(check() == 0) ans = max(ans, r - l-1);
        if(check() >= 0) {
            if(isPrime[r]) update(r, true); 
            r++;
        }
        else {
            if(isPrime[l]) update(l, false);
            ++l;
            if(l == r) {
                auto it = lower_bound(all(primes), r);
                if(it == primes.end()) break;
                r = *it;
                update(r, true);
                ++r;
            }
        }
    }
    if(check() == 0) ans = max(ans, r - l-1);
    cout << ans << endl;
    return 0;
}
0