QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#635382 | #9457. Prime Set | ucup-team4906# | AC ✓ | 339ms | 22160kb | C++20 | 3.3kb | 2024-10-12 19:38:47 | 2024-10-13 19:28:06 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define N 3010
#define M 2000010
bool vst[M];
vector<int>pri;
int n, a[N], k;
int id[M], num[M];
int S, T;
int k1, k2;
struct edge
{
int nxt, to, fl;
}e[M << 1];
int h[N], cur[N], cnt = 1;
int dep[N];
int n0;
void pre(int n)
{
for (int i = 2; i <= n; i ++)
{
if (!vst[i]) pri.push_back(i);
for (int x : pri)
{
if (1LL * x * i > n) break;
vst[x * i] = 1;
if (i % x == 0) break;
}
}
}
void add(int u, int v, int fl)
{
e[++ cnt] = (edge) {h[u], v, fl}; h[u] = cnt;
e[++ cnt] = (edge) {h[v], u, +0}; h[v] = cnt;
}
bool bfs()
{
for (int i = 1; i <= T; i ++)dep[i] = 0, cur[i] = h[i];
queue<int>q; dep[S] = 1; q.push(S);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i = h[u]; i; i = e[i].nxt) if (e[i].fl)
{
int v = e[i].to; if (dep[v])continue;
dep[v] = dep[u] + 1; q.push(v);
if (v == T)return 1;
}
}
return 0;
}
int dfs(int u, int flow)
{
if (u == T)return flow;
int res = 0;
for (int &i = cur[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dep[v] != dep[u] + 1 || (!e[i].fl))continue;
int fl = dfs(v, min(flow - res, e[i].fl));
res += fl;
e[i].fl -= fl;
e[i ^ 1].fl += fl;
if (res == flow) return res;
}
if (!dep[u]) dep[u] = 0;
return res;
}
int Dinic()
{
int sum = 0;
while (bfs()) sum += dfs(S, 1e9);
return sum;
}
void sol()
{
cin >> n >> k;
for (int i = 1; i <= n; i ++) cin >> a[i];
vector<int>A, B;
for (int i = 1; i <= n; i ++) if (!id[a[i]])
{
id[a[i]] = ++ n0;
if (a[i] % 2 == 1)A.push_back(a[i]);
else B.push_back(a[i]);
}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++) if (i != j)
{
if (!vst[a[i] + a[j]]) {k1 ++; break;}
}
}
for (int i = 1; i <= n; i ++)num[a[i]] ++;
S = n0 + 1, T = n0 + 2;
for (int x : A) if(x != 1)add(S, id[x], num[x]);
for (int y : B) add(id[y], T, num[y]);
for (int x : A) for (int y : B) if (!vst[x + y])
{
// cout << "??:" << x << ' ' << y << endl;
add(id[x], id[y], 1e9);
}
// cout << "???:" << k1 << ' ' << k2 << " " << num[1] << endl;
k2 = Dinic(); k1 -= 2 * k2;
// cout << "EE:" << k1 << ' ' << k2 << endl;
// cout << "GG:" << endl;
while (num[1])
{
add(S, id[1], 1);
int w = Dinic();
if (w) {k2 ++; k1 -= 2; num[1] --;}
else break;
}
k2 += (num[1] / 2);
k1 -= (num[1] / 2) * 2;
if (k <= k2) {cout << 2 * k << '\n';}
else {cout << 2 * k2 + min(k1, k - k2) << '\n';}
// cout << "EE:" << T << endl;
for (int i = 1; i <= T; i ++)h[i] = cur[i] = dep[i] = 0;
for (int i = 1; i <= n; i ++) num[a[i]] = id[a[i]] = 0;
k1 = k2 = 0; cnt = 1; n0 = S = T = 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
pre(M - 10);
int T = 1;
cin >> T;
while (T --) sol();
return 0;
}
/*
1
5 3
3 4 12 3 6
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 6092kb
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: 339ms
memory: 22160kb
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