QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#634032 | #9457. Prime Set | ucup-team4938# | AC ✓ | 100ms | 15196kb | C++14 | 2.8kb | 2024-10-12 16:35:56 | 2024-10-12 16:35:58 |
Judging History
answer
#include <bits/stdc++.h>
int N = 2000000, n, k, cnt = 0, pr[500010], a[3010], deg[3010];
int S, T, now[3010], dep[3010], q[3010];
bool bk[2000010], e[3010][3010], fl[3010];
void init(){
int i, j;
for(i = 2; i <= N; i++) bk[i] = true;
for(i = 2; i <= N; i++){
if(bk[i]){
pr[++cnt] = i;
}
for(j = 1; j <= cnt && i * pr[j] <= N; j++){
bk[i * pr[j]] = false;
if(i % pr[j] == 0) break;
}
}
}
int dfs1(int u, int f){
if(u == T) return f;
int v, x, res = 0;
for(v = now[u]; v <= T; v++){
now[u] = v;
if(fl[v] && e[u][v] && dep[v] == dep[u] + 1){
x = dfs1(v, 1);
if(x > 0){
e[u][v] = 0; e[v][u] = 1;
res++; f--;
if(f == 0) break;
}
}
}
return res;
}
bool bfs1(){
int i, u, v, st, nd;
for(i = 1; i <= T; i++) dep[i] = T + 1, now[i] = 1;
st = 1; nd = 1; q[1] = S; dep[S] = 0;
while(st <= nd){
u = q[st++];
for(v = 1; v <= T; v++){
if(fl[v] && dep[v] > T && e[u][v]){
dep[v] = dep[u] + 1; q[++nd] = v;
}
}
}
return dep[T] <= T;
}
int dfs2(int u, int f){
if(u == T) return f;
int v, x, res = 0;
for(v = now[u]; v <= T; v++){
now[u] = v;
if(e[u][v] && dep[v] == dep[u] + 1){
x = dfs2(v, 1);
if(x > 0){
e[u][v] = 0; e[v][u] = 1;
res++; f--;
if(f == 0) break;
}
}
}
return res;
}
bool bfs2(){
int i, u, v, st, nd;
for(i = 1; i <= T; i++) dep[i] = T + 1, now[i] = 1;
st = 1; nd = 1; q[1] = S; dep[S] = 0;
while(st <= nd){
u = q[st++];
for(v = 1; v <= T; v++){
if(dep[v] > T && e[u][v]){
dep[v] = dep[u] + 1; q[++nd] = v;
}
}
}
return dep[T] <= T;
}
void solve(){
int i, j, x = 0, y = 0, z = 0, ans = 0, cnt1 = 0;
scanf("%d%d", &n, &k);
for(i = 1; i <= n; i++) deg[i] = 0;
for(i = 1; i <= n; i++){
scanf("%d", &a[i]);
if(a[i] == 1) cnt1++;
}
for(i = 1; i <= n; i++){
for(j = 1; j < i; j++){
e[i][j] = e[j][i] = 0;
if(bk[a[i] + a[j]]){
if(a[i] & 1) e[i][j] = 1;
else e[j][i] = 1;
deg[i]++; deg[j]++;
}
}
e[i][i] = false; fl[i] = (a[i] > 1);
}
for(i = 1; i <= n; i++){
if(deg[i] > 0) y++;
}
S = n + 1; T = n + 2;
e[S][S] = e[S][T] = e[T][S] = e[T][T] = 0;
fl[S] = fl[T] = 1;
for(i = 1; i <= n; i++){
e[i][S] = e[T][i] = 0;
e[S][i] = (a[i] & 1);
e[i][T] = (a[i] & 1 ^ 1);
}
// for(i = 1; i <= T; i++){
// for(j = 1; j <= T; j++){
// if(e[i][j]) printf("%d %d\n", i, j);
// }
// }
while(bfs1()) x += dfs2(S, n);
while(bfs2()) z += dfs2(S, n);
// printf("%d %d %d\n", x, y, z);
if(k <= x + z) printf("%d\n", k << 1);
else{
ans = x + z << 1; k -= x + z;
if(k << 1 <= cnt1 - z) printf("%d\n", ans + (k << 1));
else{
ans += (cnt1 - z >> 1) << 1; k -= cnt1 - z >> 1;
printf("%d\n", std::min(y, ans + k));
}
}
}
int main()
{
init();
int t;
scanf("%d", &t);
while(t--) solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 8076kb
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: 100ms
memory: 15196kb
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