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=+;
const int masn=2e6+; 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=;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=;i<len;i++)
{
int v=vec[x][i];
if(!vis[v])
{
vis[v]=true;
if(temp[v]==||dfs(temp[v]))
{
temp[v]=x; temp[x]=v;
return true;
}
}
}
return false;
} int main()
{
Not_Prime();
cin>>t;
while(t--)
{
ans=;
memset(head,-,sizeof(head));
cin>>n>>m;
for(int i=;i<=n;i++) cin>>p[i],vec[i].clear();
memset(temp,-,sizeof(temp));
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(!prime[p[i]+p[j]])
{
vec[i].push_back(j);
vec[j].push_back(i);
temp[i]=temp[j]=;
}
int ans1=,ans2=;
for(int i=;i<=n;i++)
if(temp[i]==)
{
memset(vis,false,sizeof(vis));
if(dfs(i)) ans1++;
}
for(int i=;i<=n;i++) if(temp[i]==) ans2++;
if(ans1>=m) cout<< *m <<endl;
else cout<< ans1*+min(m-ans1,ans2) <<endl;
}
return ;
}
2017 CCPC秦皇岛 H题 Prime set的更多相关文章
- HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)
题目链接 2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块. 分块的时候满足每个块是一个 ...
- 2017 CCPC秦皇岛 A题 A Ballon Robot
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be teams parti ...
- 2017 CCPC秦皇岛 M题 Safest Buildings
PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...
- 2017 CCPC秦皇岛 L题 One Dimensions Dave
BaoBao is trapped in a one-dimensional maze consisting of grids arranged in a row! The grids are nu ...
- 2017 CCPC秦皇岛 E题 String of CCPC
BaoBao has just found a string of length consisting of 'C' and 'P' in his pocket. As a big fan of ...
- 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988
题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...
- 2017 CCPC秦皇岛 G题 Numbers
DreamGrid has a nonnegative integer . He would like to divide into nonnegative integers and minimi ...
- 2017 ccpc哈尔滨 A题 Palindrome
2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...
- HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)
题目链接 2017 CCPC Hangzhou Problem E 题意 给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...
随机推荐
- 网站搭建 - IIS 填坑 - 终于建好站了 linux + Windows
之前的IIS可以运行Windows的网页,但是对于php的网页,还是不能够支持,于是决定重新来一遍. (把踩的坑重新描述一下,在下载完php之后,解压后不要急着改文件,跳到最后的页面去改.) 以便能够 ...
- Batch批处理获取当前时间
这不是一个新问题,但是由于网上写的都是针对自己的电脑设置,没有通用性,而我呢,又需要在不同电脑上使用,因此,这命题一个问题了.其实也没有什么好说的,直接上代码. @ECHO OFF set split ...
- nyoj 16-矩形嵌套(贪心 + 动态规划DP)
16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...
- hdu 1068 Girls and Boys (最大独立集)
Girls and BoysTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【前端知识体系-JS相关】JS基础知识总结
1 变量类型和计算 1.1 值类型和引用类型的区别? 值类型:每个变量都会存储各自的值.不会相互影响 引用类型:不同变量的指针执行了同一个对象(数组,对象,函数) 1.2 typeof可以及检测的数据 ...
- 五分钟学会HTML5的WebSocket协议
1.背景 很多网站为了实现推送技术,所用的技术都是Ajax轮询.轮询是在特定的的时间间隔由浏览器对服务器发出HTTP请求,然后由服务器返回最新的数据给客户端的浏览器.这种传统的模式带来很明显的缺点 ...
- opencv 2 Opencv数据结构与基本绘图
基础图像容器Mat Mat 是一个类,又两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法不同,矩阵可以是不同的维数)的指针.矩阵头的尺 ...
- BloomFilter在Hudi中的应用
Bloom Filter在Hudi中的应用 介绍 Bloom Filter可以用于检索一个元素是否在一个集合中.它的优点是空间效率和查询时间都远远超过一般的算法,主要缺点是存在一定的误判率:当其判断元 ...
- Openlayers Projection导致经纬度颠倒问题
问题: openlayers3调用TileWMS接口,实现Openlayers加载Geoserver转发的ArcGIS切片时,web墨卡托(wkid3857)没有问题,但是WGS84(wkid4326 ...
- PostGIS 安装教程(Linux)(一)
##本文分两部分,第一部分讲linux下postgresql的安装,第二部分讲postgis的安装 ##感谢作者:https://www.linuxidc.com/Linux/2017-10/1475 ...