题解 | #素数伴侣#
素数伴侣
https://www.nowcoder.com/practice/b9eae162e02f4f928eac37d7699b352e
#include <stdio.h> #include <math.h> #include <string.h> int isp[60005]; int n,a[105],book[105],match[105]; void su(){ isp[0]=isp[1]=1; for(int i=2;i<=60000;i++) if(isp[i]==0){ for(int j=2*i;j<=60000;j+=i) isp[j]=1; } } int dfs(int x){ for(int i=0;i<n;i++){ if(a[i]%2!=0&&isp[a[i]+a[x]]==0&&book[i]==0){ book[i]=1; if(match[i]==-1||dfs(match[i])){ //printf("%d->%d\n",a[x],a[i]); match[i]=x; match[x]=i; return 1; } } } return 0; } int main() { int count=0; memset(isp,0,sizeof(isp)); su(); scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } memset(match,-1,sizeof(match)); for(int i=0;i<n;i++){ if(a[i]%2==0){ memset(book,0,sizeof(book)); book[i]=1; if(dfs(i)){ count++; } } } printf("%d",count); return 0; }