結果

問題 No.12 限定された素数
ユーザー penguinshunyapenguinshunya
提出日時 2020-06-05 01:57:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 720 ms / 5,000 ms
コード長 3,692 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 212,032 KB
実行使用メモリ 8,712 KB
最終ジャッジ日時 2023-08-20 19:55:20
合計ジャッジ時間 14,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 407 ms
8,496 KB
testcase_01 AC 408 ms
8,580 KB
testcase_02 AC 408 ms
8,436 KB
testcase_03 AC 720 ms
8,612 KB
testcase_04 AC 407 ms
8,528 KB
testcase_05 AC 422 ms
8,540 KB
testcase_06 AC 463 ms
8,564 KB
testcase_07 AC 446 ms
8,560 KB
testcase_08 AC 438 ms
8,496 KB
testcase_09 AC 409 ms
8,564 KB
testcase_10 AC 419 ms
8,544 KB
testcase_11 AC 473 ms
8,552 KB
testcase_12 AC 448 ms
8,436 KB
testcase_13 AC 427 ms
8,440 KB
testcase_14 AC 413 ms
8,572 KB
testcase_15 AC 425 ms
8,572 KB
testcase_16 AC 471 ms
8,712 KB
testcase_17 AC 417 ms
8,576 KB
testcase_18 AC 405 ms
8,496 KB
testcase_19 AC 406 ms
8,476 KB
testcase_20 AC 407 ms
8,568 KB
testcase_21 AC 419 ms
8,548 KB
testcase_22 AC 407 ms
8,584 KB
testcase_23 AC 409 ms
8,568 KB
testcase_24 AC 408 ms
8,640 KB
testcase_25 AC 418 ms
8,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#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 reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int) v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

void yes() { cout << "Yes" << endl; exit(0); }
void no() { cout << "No" << endl; exit(0); }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> void amax(T &x, T y) { x = max(x, y); }
template <typename T> void amin(T &x, T y) { x = min(x, y); }
template <typename T> T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

const int INF = 1001001001;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct Prime {
  int n;
  vector<int> pri;
  Prime(int n) : n(n) {
    vector<bool> f(n, true);
    f[0] = f[1] = false;
    for (int i = 2; i < n; i++) {
      if (!f[i]) continue;
      pri.push_back(i);
      for (int j = i * 2; j < n; j += i) {
        f[j] = false;
      }
    }
  }
  map<int, int> factor(int k) {
    map<int, int> so;
    for (auto p : pri) {
      while (k % p == 0) k /= p, so[p]++;
    }
    if (k != 1) so[k] = 1;
    return so;
  }
};

auto prime = Prime(5000001).pri;

template <typename T>
struct SegmentTree {
  vector<T> v;
  function<T(T, T)> f;
  T e;
  int n;
  SegmentTree(int size, function<T(T, T)> f, T e) : f(f), e(e) {
    n = 1;
    while (n < size) n <<= 1;
    v.resize(n * 2, e);
  }
  void set(int k, T x) {
    v[k + n] = x;
  }
  void build() {
    for (int i = n - 1; i > 0; i--) {
      v[i] = f(v[i * 2 + 0], v[i * 2 + 1]);
    }
  }
  void update(int k, T x) {
    v[k += n] = x;
    while (k >>= 1) v[k] = f(v[k * 2 + 0], v[k * 2 + 1]);
  }
  T query(int a, int b) {
    T l = e, r = e;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) l = f(l, v[a++]);
      if (b & 1) r = f(v[--b], r);
    }
    return f(l, r);
  }
  T operator[](int k) {
    return query(k, k + 1);
  }
};

void solve() {
  int n;
  cin >> n;
  int bit = 0;
  rep(i, n) {
    int a;
    cin >> a;
    bit |= 1 << a;
  }
  n = sz(prime);
  SegmentTree<int> st(n, [](int a, int b) { return a | b; }, 0);
  rep(i, n) {
    int p = prime[i];
    int b = 0;
    while (p) b |= 1 << (p % 10), p /= 10;
    st.set(i, b);
  }
  st.build();
  int ans = -1;
  rep(k, n) {
    int ok = k - 1, ng = n + 1;
    while (abs(ok - ng) != 1) {
      int x = (ok + ng) / 2;
      (st.query(k, x) <= bit ? ok : ng) = x;
    }
    if (st.query(k, ok) == bit) {
      int l = k == 0 ? 1 : prime[k - 1] + 1;
      int r = ok == n ? 5000000 : prime[ok] - 1;
      amax(ans, r - l);
    }
  }
  cout << ans << endl;
}
0