素数伴侣
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>
#include <set>
#include<stdio.h>
using namespace std;
bool isprimer(int x, int y)
{
int sum = x + y;
if (sum % 2 == 0)
return false;
for (int i = 3; i < sqrt(sum); i = i + 2)
{
if (sum%i == 0)
return false;
}
return true;
}
vector<int> nums;
int visit[210];
int match[210];
int DFS(int x)
{
//使用二分图匹配算法
int len = nums.size();
for (int i = 0; i < len; ++i)
{
if (nums[i] % 2 != 0)//只选择奇数
{
if (visit[i] == 0&&isprimer(nums[x],nums[i]))
{
visit[i] = 1;
if (match[i] == -1 || DFS(match[i]))
{
match[i] = x;
match[x] = i;
return 1;
}
}
}
}
return 0;
}
int main()
{
int n;
while(cin >> n)
{
nums.clear();
int sum=0;
memset(match, -1, sizeof(match));
//int sum = 0;
int temp;
while (n)
{
cin >> temp;
nums.push_back(temp);
--n;
}
int len = nums.size();
for (int i = 0; i < len; ++i)
{
if (nums[i] % 2 == 0)
{
memset(visit, 0, sizeof(visit));
visit[i] = 1;
if (DFS(i))
++sum;
}
}
//printf("%d",len);
//printf("%d\n",sum);
printf("%d\n",sum);
}
}