博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017 CCPC秦皇岛 H题 Prime set
阅读量:4973 次
发布时间:2019-06-12

本文共 3004 字,大约阅读时间需要 10 分钟。

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

44 22 3 4 55 33 4 12 3 66 31 3 6 8 1 11 01

<h4< dd="">Sample Output

4360

<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);

参考代码:

1 #include
2 using namespace std; 3 const int maxn=3000+10; 4 const int masn=2e6+10; 5 6 int t,n,m,ans,head[maxn],p[maxn],temp[maxn]; 7 bool vis[maxn],prime[masn]; 8 vector
vec[maxn]; 9 10 void Not_Prime()11 {12 for(int i=2;i
>t;42 while(t--)43 {44 ans=1;45 memset(head,-1,sizeof(head));46 cin>>n>>m;47 for(int i=1;i<=n;i++) cin>>p[i],vec[i].clear();48 memset(temp,-1,sizeof(temp));49 for(int i=1;i<=n;i++)50 for(int j=i+1;j<=n;j++)51 if(!prime[p[i]+p[j]])52 {53 vec[i].push_back(j);54 vec[j].push_back(i);55 temp[i]=temp[j]=0;56 }57 int ans1=0,ans2=0;58 for(int i=1;i<=n;i++)59 if(temp[i]==0)60 {61 memset(vis,false,sizeof(vis));62 if(dfs(i)) ans1++;63 }64 for(int i=1;i<=n;i++) if(temp[i]==0) ans2++;65 if(ans1>=m) cout<< 2*m <
View Code

 

转载于:https://www.cnblogs.com/songorz/p/9787757.html

你可能感兴趣的文章