题目描述
Given an array of n integers a1,a2,…,an, we say a set {i,j} is a prime set of the given array, if i≠j and ai+aj is prime.
BaoBao has just found an array of n integers a1,a2,…,an in his pocket. He would like to select at most k 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 m and p1,p2,…,pm, where m≤k and pi is a prime set of the given array. Please help BaoBao calculate the maximum size of the union set.
输入
There are multiple test cases. The first line of the input is an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and k (), their meanings are described above.
The second line contains n integers a1,a2,…,an(1≤ai≤106), indicating the given array.
It’s guaranteed that the sum of n over all test cases will not exceed 104.
输出
For each test case output one line containing one integer, indicating the maximum size of the union of at most k prime set of the given array.
样例输入
样例输出
提示
For the first sample test case, there are 3 prime sets: {1, 2}, {1, 4} and {2, 3}. As k=2, 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 k=3, 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 k=3, 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个数,如果两数之和()为素数,那么这两个数的下标组成的集合
问最多挑选k个这样的集合,集合最大的大小是多少
思路:
将给出的n个数进行暴力两两求和,看两数之和是否为素数,将能够构成素数的两个数的下标连一条边
开始将match数组标记为,如果能够构成素数,那么将这个数标记为
然后进行二分图最大匹配,如果说匹配的个数 那么答案就是
如果说匹配的个数 ,首先设置能够另外加的贡献为,那么答案为 的个数,其中
注意要将match进行双向标记,而且在进入函数后进行标记