QOJ.ac

QOJ

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

Judging History

你现在查看的是最新测评结果

  • [2024-10-13 19:27:57]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:10ms
  • 内存:9764kb
  • [2024-10-13 19:27:24]
  • hack成功,自动添加数据
  • (/hack/967)
  • [2024-10-13 18:38:53]
  • 管理员手动重测本题所有得分≥97分的提交记录
  • 测评结果:100
  • 用时:11ms
  • 内存:11068kb
  • [2024-10-13 18:24:51]
  • hack成功,自动添加数据
  • (/hack/965)
  • [2024-10-13 18:17:54]
  • hack成功,自动添加数据
  • (/hack/963)
  • [2024-10-12 18:10:43]
  • 评测
  • 测评结果:100
  • 用时:8ms
  • 内存:10568kb
  • [2024-10-12 18:10:43]
  • 提交

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;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 6060kb

input:

4
4 2
2 3 4 5
5 3
3 4 12 3 6
6 3
1 3 6 8 1 1
1 0
1

output:

4
3
6
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 10ms
memory: 9764kb

input:

110
3 3
2 2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
4 2
1 1 1 1
10 7
1 1 1 2 4 6 8 10 12 14
18 1
10 5 9 4 10 7 2 4 9 1 6 1 4 9 2 4 9 7
19 5
1 6 1 8 8 1 5 10 9 2 10 2 1 9 6 10 4 3 6
14 4
6 6 8 9 5 3 4 9 2 2 1 10 10 1
15 10
5 4 2 4 10 1 8 4 10 3 10 3 7 10 4
17 5
10 7 2 4 3 1 10 8 2 8 3 4 1 1 1 1 1
20 6
8 4 6 ...

output:

0
2
3
3
4
8
2
10
8
15
10
12
10
10
12
16
10
10
12
16
14
13
13
14
17
0
19
12
12
11
16
10
19
2
12
10
5
14
10
10
13
2
18
2
4
4
11
8
11
8
0
10
10
0
10
0
8
15
12
11
13
18
10
17
9
8
20
19
13
6
4
6
11
9
13
11
6
2
8
12
17
14
6
2
20
2
18
17
6
2
10
0
7
16
2
2
0
2
18
14
11
8
4
6
0
19
890
204
2746
2372

result:

ok 110 lines

Extra Test:

score: 0
Extra Test Passed