結果

問題 No.12 限定された素数
ユーザー T1610T1610
提出日時 2019-03-17 21:03:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,077 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 168,724 KB
実行使用メモリ 24,720 KB
最終ジャッジ日時 2023-09-22 02:32:22
合計ジャッジ時間 9,210 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 205 ms
24,520 KB
testcase_01 AC 218 ms
24,496 KB
testcase_02 WA -
testcase_03 AC 261 ms
24,384 KB
testcase_04 WA -
testcase_05 AC 244 ms
24,500 KB
testcase_06 WA -
testcase_07 AC 230 ms
24,488 KB
testcase_08 AC 221 ms
24,380 KB
testcase_09 AC 229 ms
24,564 KB
testcase_10 WA -
testcase_11 AC 247 ms
24,496 KB
testcase_12 AC 289 ms
24,332 KB
testcase_13 AC 260 ms
24,500 KB
testcase_14 WA -
testcase_15 AC 260 ms
24,600 KB
testcase_16 AC 315 ms
24,564 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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;

/*
  エラトステネスの篩
  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 = 2, ans = -1;
    vector<int> cnt(10, 0);
    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) {
        if(check() == 0) ans = max(ans, r - l);
        if(check() >= 0) {
            r++;
            if(isPrime[r]) update(r, true); 
        }
        else {
            if(isPrime[l]) update(l, false);
            ++l;
            if(l == r) {
                auto it = upper_bound(all(primes), r);
                if(it == primes.end()) break;
                r = *it;
                update(r, true);
            }
        }
    }
    if(check() == 0) ans = max(ans, r - l);
    cout << ans << endl;
    return 0;
}
0