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 .
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.
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
Sample Output
4 3 6 0
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.
4 5
1 8 3 1
4 2
1 1 6 2
6 2
1 1 6 2 1 1
4 2
1 1 4 6
3 5
1 8 3
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 1e6+10;
const int M = 3000+100;
int a[M];
int prime[MAXN*2],cnt; //0表示素数
vector<int> map[M];
//int visit[M];
int f[M],n,k;
int B[M],resn;
void primeTable()
{
cnt=0;
memset(prime,0,sizeof(prime));
prime[0]=prime[1]=1;
for(int i=2;i<MAXN*2 ;i++)
{
if(!prime[i])
{
for(int j=i+i;j<MAXN*2 ;j+=i)
prime[j]=1;
}
}
}
int Hungary(int x)
{
//f[x]=1;
for(int i=0;i<map[x].size();i++) //!!存图用vector,原因用矩阵存每次要遍历n个点,时间复杂度太大了
{
int u=map[x][i];
if(f[u]==0)
{
f[u]=1;
if(B[u]==-1||Hungary(B[u]))
{
B[u]=x;
B[x]=u;
return 1;
}
}
}
return 0;
}
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int t,nonev;
scanf("%d",&t);
primeTable();
while(t--)
{
int num=0;
nonev=0;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
B[i]=-2;
map[i].clear();
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++) //建立二分图
{
for(int j=i+1;j<n;j++)
{
if(!prime[a[i]+a[j]])
{
B[i]=B[j]=-1;
if(a[i]==1&&a[j]==1) continue;
if(a[i]%2)
map[i].push_back(j);
else
map[j].push_back(i);
}
}
}
int ans=0;
for(int i=0;i<n;i++)
{
if(a[i]%2&&B[i]==-1) //只对奇数且度数不为0的点进行搜索
{
memset(f,0,sizeof(f));
if(Hungary(i))
{
ans++;
}
}
}
int single=0;
for(int i=0;i<n;i++)
{
if((B[i]==-2||B[i]==-1)&&a[i]==1) //单个的1
{
single++;
}
if(B[i]==-2) //度数为0的点
{
nonev++;
}
}
ans+=single/2; //二元组个数
resn=n-nonev-ans*2; //剩余单元组个数
if(ans>=k)
{
printf("%d\n",k*2);
}
else
{
if(k-ans>resn)
{
printf("%d\n",ans*2+resn);
}
else
{
printf("%d\n",ans+k);
}
}
}
return 0;
}