QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#639142 | #9457. Prime Set | supepapupu | WA | 354ms | 18684kb | C++20 | 3.6kb | 2024-10-13 18:04:48 | 2024-10-13 18:04:49 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 3010, M = 1e7 + 10, INF = 0x3f3f3f3f, mod = 998244353;
namespace MF {
const int INF = 1e8;
int S, T = N - 1;
int h[N], e[M], f[M], ne[M], idx;
int q[N], d[N], cur[N];
void clear() {
memset(h, -1, sizeof h), idx = 0;
}
void add_edge(int a, int b, int c) {
// cout << a << ' ' << b << ' ' << c << el;
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
int hh = 0, tt = 0;
memset(d, -1, sizeof d);
q[0] = S, d[S] = 0, cur[S] = h[S];
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if (d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if (ver == T) return 1;
q[++tt] = ver;
}
}
}
return 0;
}
int find(int u, int limit) {
if (u == T) return limit;
int flow = 0;
for (int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if (d[ver] == d[u] + 1 && f[i]) {
int t = find(ver, min(f[i], limit - flow));
if (!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic() {
int r = 0, flow;
while (bfs()) while (flow = find(S, INF)) r += flow;
return r;
}
}
int primes[2000010], idx;
bool st[2000010];
// 线性筛质数
void sieve(int n) {
for (int i = 2; i <= n; ++i) {
if (!st[i]) primes[idx++] = i;
for (int j = 0; primes[j] <= n / i; ++j) {
st[i * primes[j]] = 1;
if (i % primes[j] == 0) break;
}
}
}
void solve() {
MF::clear();
int n, k; cin >> n >> k;
int one = 0;
vector<int> a(n + 1);
MF::add_edge(MF::S, N - 2, 0);
map<int, int> odd, even;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == 1) ++one;
else {
if (a[i] & 1) ++odd[a[i]];
else ++even[a[i]];
}
}
int valid = 0;
for (int i = 1; i <= n; ++i) {
int t = 0;
for (int j = 1; j <= n; ++j)
if (!st[a[i] + a[j]]) {
t = 1; break;
}
valid += t;
}
int tot = 0;
map<int, int> mp;
for (auto[a, b]: odd) {
mp[a] = ++tot;
MF::add_edge(MF::S, tot, b);
}
for (auto[a, b]: even) {
mp[a] = ++tot;
MF::add_edge(tot, MF::T, b);
if (!st[1 + a]) MF::add_edge(N - 2, tot, INF);
}
for (auto[a, b]: odd) for (auto[c, d]: even) {
if (!st[a + c]) MF::add_edge(mp[a], mp[c], b + d);
}
int two = MF::dinic();
for (int i = 1; i <= one; ++i) {
++MF::f[0];
int t = MF::dinic();
if (t) ++two;
else {
two += (one - i + 1) / 2;
break;
}
}
two = min(two, k);
cout << min(two * 2 + (k - two), valid) << el;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
sieve(2e6);
int tcase = 1;
cin >> tcase;
while (tcase--) solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 14056kb
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: 354ms
memory: 18684kb
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