QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#633411 | #9457. Prime Set | ucup-team3931# | AC ✓ | 178ms | 11844kb | C++14 | 3.4kb | 2024-10-12 15:18:46 | 2024-10-12 15:18:46 |
Judging History
answer
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
bool Mst;
const int maxn = 3030;
const int maxm = 2000100;
const int N = 2000000;
int n, m, a[maxn], b[maxn], tot, pr[maxm / 10], K;
bitset<maxm> vis;
inline void init() {
vis[0] = vis[1] = 1;
for (int i = 2; i <= N; ++i) {
if (!vis[i]) {
pr[++tot] = i;
}
for (int j = 1; j <= tot && i * pr[j] <= N; ++j) {
vis[i * pr[j]] = 1;
if (i % pr[j] == 0) {
break;
}
}
}
}
int id[maxn], S, T, nt, hd[maxn * 10], len;
struct edge {
int next;
short to, cap;
} edges[4600000];
inline void add_edge(int u, int v, int c) {
edges[++len].to = v;
edges[len].next = hd[u];
edges[len].cap = c;
hd[u] = len;
}
struct Dinic {
int d[maxn * 10], cur[maxn * 10];
bool vis[maxn * 10];
inline void add(int u, int v, int c) {
add_edge(u, v, c);
add_edge(v, u, 0);
}
inline bool bfs() {
queue<int> q;
for (int i = 1; i <= nt; ++i) {
vis[i] = 0;
d[i] = -1;
}
q.push(S);
vis[S] = 1;
d[S] = 0;
while (q.size()) {
int u = q.front();
q.pop();
for (int i = hd[u]; i; i = edges[i].next) {
edge &e = edges[i];
if (!vis[e.to] && e.cap) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return vis[T];
}
short dfs(int u, short a) {
if (u == T || !a) {
return a;
}
short flow = 0, f;
for (int &i = cur[u]; i; i = edges[i].next) {
edge &e = edges[i];
if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap))) > 0) {
e.cap -= f;
edges[i ^ 1].cap += f;
flow += f;
a -= f;
if (!a) {
break;
}
}
}
return flow;
}
inline int solve() {
int flow = 0;
while (bfs()) {
for (int i = 1; i <= nt; ++i) {
cur[i] = hd[i];
}
flow += dfs(S, 15000);
}
return flow;
}
} solver;
void solve() {
for (int i = 0; i <= nt; ++i) {
hd[i] = 0;
}
nt = 0;
len = 1;
S = ++nt;
T = ++nt;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
K = 0;
for (int i = 1; i <= n; ++i) {
bool fl = 0;
for (int j = 1; j <= n && !fl; ++j) {
fl |= (i != j && !vis[a[i] + a[j]]);
}
if (fl) {
b[++K] = a[i];
}
}
for (int i = 1; i <= K; ++i) {
id[i] = ++nt;
if (b[i] == 1) {
continue;
}
if (b[i] & 1) {
solver.add(S, id[i], 1);
} else {
solver.add(id[i], T, 1);
}
}
for (int i = 1; i <= K; ++i) {
if (b[i] % 2 == 0) {
continue;
}
for (int j = 1; j <= K; ++j) {
if (b[j] % 2 == 0 && !vis[b[i] + b[j]]) {
solver.add(id[i], id[j], 1);
}
}
}
int flow = solver.solve();
for (int i = hd[S]; i; i = edges[i].next) {
edges[i].cap = edges[i ^ 1].cap = 0;
}
int cnt = 0;
for (int i = 1; i <= K; ++i) {
if (b[i] == 1) {
solver.add(S, id[i], 1);
++cnt;
}
}
int t = solver.solve();
flow += t + (cnt - t) / 2;
if (flow >= m) {
printf("%d\n", m * 2);
return;
}
printf("%d\n", min(K, flow * 2 + m - flow));
}
bool Med;
int main() {
fprintf(stderr, "%.2lf MB\n", (&Mst - &Med) / 1048576.);
init();
int T = 1;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 9ms
memory: 4744kb
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: 178ms
memory: 11844kb
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