# 2017CCPC秦皇岛H(二分 + 二分图的最大匹配数)

Prime Set

Time Limit: 2 Seconds      Memory Limit: 131072 KB

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.

Author: WENG, Caizhi

Source: The 2017 China Collegiate Programming Contest, Qinhuangdao site

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000000 + 10;
int n, k;
int a[3010];
int odd[3010];
int even[3010];
int oddnum, evennum;
bool valid[maxn];
int prime[maxn];
vector<int> g[3010];
bool visit[3 * 3010];
int Match[3 * 3010];
int One;
int flag;
void initPrime()
{
memset(valid, true, sizeof(valid));
int tot = 0;
for(int i = 2; i <= 2000000; i++)
{
if(valid[i])
{
prime[++tot] = i;
}
for(int j = 1; j <= tot && prime[j] * i <= 2000000; j++)
{
valid[i * prime[j]] = false;
if(i % prime[j] == 0) break;
}
}
}
void init()
{
for(int i = 1; i < 3010; i++) g[i].clear();
memset(Match, -1, sizeof(Match));
oddnum = 0;
evennum = 0;
One = 0;
}
bool dfs(int u)
{
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if(visit[v]) continue;
if(Match[v] == -1)
{
Match[v] = u;
return true;
}
visit[v] = true;
if(dfs(Match[v]))
{
Match[v] = u;
return true;
}
}
return false;
}
int getMatch(int up)
{
int x = 0;
memset(Match, -1, sizeof(Match));
for(int i = 1; i <= up; i++)
{
memset(visit, false, sizeof(visit));
if(dfs(i)) x++;
}
return x;
}
bool judge(int mid, int x)
{
for(int i = oddnum + 1; i <= oddnum + mid; i++)
{
g[i].clear();
for(int j = 1; j <= evennum; j++)
{
if(valid[1 + even[j]]) g[i].push_back(j);
}
}
int num = getMatch(oddnum + mid);
if(num - x >= mid && num) return true;
else return false;
}
int solve()
{
int ori = getMatch(oddnum);//不加1时的匹配数
int l = 0, r = One;
int re = 0;
while(l <= r)
{
int mid = (l + r)>>1;
if(judge(mid, ori))
{
re = mid;
l = mid + 1;
}
else r = mid - 1;
}
for(int i = oddnum + 1; i <= oddnum + re; i++)
{
g[i].clear();
for(int j = 1; j <= evennum; j++)
{
if(valid[1 + even[j]]) g[i].push_back(j);
}
}
int ans = getMatch(oddnum + re);
if(ans >= k) return 2 * k;
else
{
One -= re;
flag = One;
One /= 2;
k -= ans;
ans *= 2;
if(One >= k) return ans + 2 * k;
else
{
ans += 2 * One;
k -= One;
memset(visit, false, sizeof(visit));
for(int i = 1; i <= evennum; i++)
{
if(Match[i] != -1)
{
int v = Match[i];
visit[v] = true;
}
}
for(int i = 1; i <= oddnum + re; i++)
{
for(int j = 0; j < g[i].size(); j++)
{
if(!visit[i])
{
if(k)
{
k--;
ans++;
visit[i] = true;
}
else break;
}
int v = g[i][j];
if(Match[v] == -1)
{
if(k)
{
k--;
ans++;
Match[v] = i;
}
else break;
}
}
if(k == 0) break;
}
if(k)
{
if(flag == 1)
{
for(int i = 1; i <= evennum; i++)
{
if(valid[1 + even[i]])
{
ans++;
break;
}
}
}
else if(flag&1) ans++;
}
}
return ans;
}
}
int main()
{
//freopen("C:\\Users\\creator\\Desktop\\in1.txt","r",stdin) ;
//freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout) ;
int T;
scanf("%d", &T);
initPrime();
while(T--)
{
scanf("%d%d", &n, &k);
One = 0;
init();
int ans = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(a[i] == 1) One++;
else if(a[i]&1) odd[++oddnum] = a[i];
else even[++evennum] = a[i];
}
for(int i = 1; i <= oddnum; i++)
{
for(int j = 1; j <= evennum; j++)
{
if(valid[odd[i] + even[j]])
{
g[i].push_back(j);
}
}
}
printf("%d\n", solve());
}
return 0;
}