QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#972#634821#9457. Prime Setucup-team087ucup-team4435Failed.2024-10-13 21:47:302024-10-13 21:47:31

Details

Extra Test:

Accepted
time: 11ms
memory: 8764kb

input:

4
3000 1499
41 42 251 252 461 462 671 672 881 882 1091 1092 1301 1302 1511 1512 1721 1722 1931 1932 2141 2142 2351 2352 2561 2562 2771 2772 2981 2982 3191 3192 3401 3402 3611 3612 3821 3822 4031 4032 4241 4242 4451 4452 4661 4662 4871 4872 5081 5082 5291 5292 5501 5502 5711 5712 5921 5922 6131 6132 ...

output:

2998
3000
3000
1000

result:

ok 4 lines

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#634821#9457. Prime Setucup-team4435#AC ✓10ms9764kbC++234.7kb2024-10-12 18:10:432024-10-13 19:27:57

answer

#pragma GCC optimize("Ofast")

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
    --l;
    while (r - l > 1) {
        T mid = l + (r - l) / 2;
        if (predicat(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return r;
}


template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
    return FindFirstTrue(l, r, predicat) - 1;
}


const ll INF = 2e18;
const int INFi = 2e9;
const int N = 2e6 + 5;
const int LG = 20;

bool pr[N];

void precalc() {
    for(int i = 2; i < N; ++i) pr[i] = true;
    for(int i = 2; i < N; ++i) {
        if (!pr[i]) continue;
        if (1ll * i * i >= N) continue;
        for(int j = i * i; j < N; j += i) {
            pr[j] = false;
        }
    }
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vi L, R;

int was[N];

const int M = 3e3 + 5;
int mt[M];
int T = 1;

int sz;

bool kuhn(int v) {
    int x = L[v];
    if (was[x] == T) return false;
    was[x] = T;

    rep(i, sz) {
        if (mt[i] == -1 && pr[x + R[i]]) {
            mt[i] = v;
            return true;
        }
    }
    rep(i, sz) {
        if (pr[x + R[i]] && kuhn(mt[i])) {
            mt[i] = v;
            return true;
        }
    }

    return false;
}

void solve() {

    L.clear();
    R.clear();

    int n, k; cin >> n >> k;
    int cnt1 = 0;
    vi L2;

    vi oL, oR;
    rep(i, n) {
        int x; cin >> x;
        if (x == 1) {
            cnt1++;
            continue;
        }
        if (x % 2 == 0) {
            if (pr[x + 1]) {
                L2.push_back(x);
            } else {
                L.push_back(x);
            }
        } else {
            R.push_back(x);
        }
    }
    shuffle(all(L), rng);
    shuffle(all(L2), rng);
    for(auto &x : L2) L.push_back(x);
    shuffle(all(R), rng);
    rep(i, R.size()) mt[i] = -1;

    sz = R.size();

    int was1 = cnt1;

    set<int> bad;
    int total = 0;
    int qq = 0;
    rep(i, L.size()) {
        if (qq != (int)R.size() && !bad.contains(L[i])) {
            T++;
            if (!kuhn(i)) {
                bad.insert(L[i]);
            } else {
                qq++;
                total++;
                continue;
            }
        }
        if (pr[L[i] + 1] && cnt1) {
            total++;
            cnt1--;
        } else {
            oL.push_back(L[i]);
        }
    }
    rep(i, R.size()) {
        if (mt[i] == -1) {
            oR.push_back(R[i]);
        }
    }

    if (was1) {
        R.push_back(1);
    }

    total += cnt1 / 2;
    cnt1 %= 2;
    rep(_, cnt1) oR.push_back(1);

    if (k <= total) {
        cout << k * 2 << '\n';
        return;
    }

    int ans = total * 2;
    k -= total;

    for(auto &x : oL) {
        if (!k) break;
        bool ok = false;
        for(auto &y : R) {
            if (pr[x + y]) {
                ok = true;
                break;
            }
        }
        if (ok) {
            k--;
            ans++;
        }
    }

    for(auto &x : oR) {
        if (!k) break;
        bool ok = false;
        for(auto &y : L) {
            if (pr[x + y]) {
                ok = true;
                break;
            }
        }
        if (ok || (x == 1 && was1 > 1)) {
            k--;
            ans++;
        }
    }
    cout << ans << '\n';

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    precalc();
    cin >> t;
    rep(i, t) {
        solve();
    }
    return 0;
}