2017 CCPC秦皇岛 H题 Prime set

Given an array of  integers , we say a set  is a prime set of the given array, if  and  is prime.

BaoBao has just found an array of  integers  in his pocket. He would like to select at most  prime set of that array to maximize the size of the union of the selected sets. That is to say, to maximize  by carefully selecting and , where  and  is a prime set of the given array. Please help BaoBao calculate the maximum size of the union set.

Input

There are multiple test cases. The first line of the input is an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), their meanings are described above.

The second line contains  integers  (), indicating the given array.

It's guaranteed that the sum of  over all test cases will not exceed .

<h4< dd="">Output

For each test case output one line containing one integer, indicating the maximum size of the union of at most  prime set of the given array.

<h4< dd="">Sample 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

<h4< dd="">Sample Output

4
3
6
0

<h4< dd="">Hint

For the first sample test case, there are 3 prime sets: {1, 2}, {1, 4} and {2, 3}. As , we can select {1, 4} and {2, 3} to get the largest union set {1, 2, 3, 4} with a size of 4.

For the second sample test case, there are only 2 prime sets: {1, 2} and {2, 4}. As , we can select both of them to get the largest union set {1, 2, 4} with a size of 3.

For the third sample test case, there are 7 prime sets: {1, 3}, {1, 5}, {1, 6}, {2, 4}, {3, 5}, {3, 6} and {5, 6}. As , we can select {1, 3}, {2, 4} and {5, 6} to get the largest union set {1, 2, 3, 4, 5, 6} with a size of 6.

题解:题意是给你n个数,然后让你找满足<x,y> x+y为素数这样的二元集合元素的交集,且集合的数量不超过m个;

我们可以先筛选出素数,然后暴力匹配,跑出每一个数字可以和哪些其他的数字组合成素数;

然后我们跑二分图最大匹配ans,得到的这些元素对<a,b> <c,d>中的元素各部相等,判断一下,集合的数量是否大于等于m;如果是,则输出2*m;

否则,我们统计没有匹配的数的数量ans2,然后答案等于 ans*2+min(ans2,m-ans);

参考代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3000+10;
const int masn=2e6+10;

int t,n,m,ans,head[maxn],p[maxn],temp[maxn];
bool vis[maxn],prime[masn];
vector<int> vec[maxn];

void Not_Prime()
{
    for(int i=2;i<masn;i++)
        if(!prime[i])
          for(int j=i+i;j<masn;j+=i)
            prime[j]=true;
}

bool dfs(int x)
{
    vis[x]=true;
    int len=vec[x].size();
    for(int i=0;i<len;i++)
    {
        int v=vec[x][i];
        if(!vis[v])
        {
            vis[v]=true;
            if(temp[v]==0||dfs(temp[v]))
            {
                temp[v]=x; temp[x]=v;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    Not_Prime();
    cin>>t;
    while(t--)
    {
        ans=1;
        memset(head,-1,sizeof(head));
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>p[i],vec[i].clear();
        memset(temp,-1,sizeof(temp));
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(!prime[p[i]+p[j]])
                {
                    vec[i].push_back(j);
                    vec[j].push_back(i);
                    temp[i]=temp[j]=0;
                }
        int ans1=0,ans2=0;
        for(int i=1;i<=n;i++)
            if(temp[i]==0)
            {
                memset(vis,false,sizeof(vis));
                if(dfs(i)) ans1++;
            }
        for(int i=1;i<=n;i++) if(temp[i]==0) ans2++;
        if(ans1>=m) cout<< 2*m <<endl;
        else cout<< ans1*2+min(m-ans1,ans2) <<endl;
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songorz/p/9787757.html