QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#666641#9457. Prime Setucup-team4479AC ✓38ms7884kbC++233.3kb2024-10-22 19:27:382024-10-22 19:28:18

Judging History

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

  • [2024-10-22 19:28:18]
  • 评测
  • 测评结果:AC
  • 用时:38ms
  • 内存:7884kb
  • [2024-10-22 19:27:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int N=3005,M=2000005;
bool isprime[M];
void init(int n=2000000)
{
    fill(isprime+1,isprime+n+1,true);
    isprime[1]=false;
    for(int i=2;i<=n;i++)
        if(isprime[i])
        {
            for(int j=i+i;j<=n;j+=i)
                isprime[j]=false;
        }
    return;
}
struct Hopcroft_Karp
{
    int n,m;
    vector<int>g[N];
    int pa[N],pb[N];
    Hopcroft_Karp(){}
    Hopcroft_Karp(int _n,int _m):n(_n),m(_m){}
    void init(int _n,int _m)
    {
        n=_n,m=_m;
        for(int i=1;i<=n;i++)
            g[i].clear();
        return;
    }
    void add_edge(int u,int v)
    {
        g[u].push_back(v);
        return;
    }
    int dis[N];
    bool bfs()
    {
        queue<int>q;
        for(int u=1;u<=n;u++)
        {
            if(!pa[u])
            {
                dis[u]=0;
                q.push(u);
            }
            else dis[u]=-1;
        }
        bool found=false;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int v:g[u])
            {
                if(pb[v]&&dis[pb[v]]==-1)
                {
                    dis[pb[v]]=dis[u]+1;
                    q.push(pb[v]);
                }
                else if(!pb[v]) found=true;
            }
        }
        return found;
    }
    bool dfs(int u)
    {
        for(int v:g[u])
            if(!pb[v]||(dis[pb[v]]==dis[u]+1&&dfs(pb[v])))
            {
                pa[u]=v,pb[v]=u;
                return true;
            }
        dis[u]=-1;
        return false;
    }
    int max_matching()
    {
        fill(pa+1,pa+n+1,0);
        fill(pb+1,pb+m+1,0);
        int res=0;
        while(bfs())
        {
            for(int u=1;u<=n;u++)
                if(!pa[u]&&dfs(u)) res++;
        }
        return res;
    }
};
int n,k;
int a[N];
int vl[N],totl;
int vr[N],totr;
Hopcroft_Karp solver;
void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    totl=totr=0;
    for(int i=1;i<=n;i++)
        if(a[i]&1) vl[++totl]=a[i];
        else vr[++totr]=a[i];
    solver.init(totl,totr);
    for(int i=1;i<=totl;i++)
        for(int j=1;j<=totr;j++)
            if(isprime[vl[i]+vr[j]]) solver.add_edge(i,j);
    int y=solver.max_matching();
    totl=totr=0;
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(a[i]>1)
        {
            if(a[i]&1) vl[++totl]=a[i];
            else vr[++totr]=a[i];
        }
        else cnt++;
    solver.init(totl,totr);
    for(int i=1;i<=totl;i++)
        for(int j=1;j<=totr;j++)
            if(isprime[vl[i]+vr[j]]) solver.add_edge(i,j);
    int x=solver.max_matching();
    int m1=y+(cnt-(y-x))/2;
    int m2=0;
    for(int i=1;i<=n;i++)
    {
        bool flag=false;
        for(int j=1;j<=n;j++)
            if(i!=j&&isprime[a[i]+a[j]])
            {
                flag=true;
                break; 
            }
        if(flag) m2++;
    }
    int ans=k;
    ans+=min(m1,k);
    ans=min(ans,m2);
    cout<<ans<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    init();
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

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

詳細信息

Test #1:

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

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: 38ms
memory: 7884kb

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