CSU 1552: Friends(素数测试+二分匹配)

来源:互联网 发布:最优化 中科院 编辑:程序博客网 时间:2024/04/24 18:38

Description

On an alien planet, every extraterrestrial is born with a number. If the sum of two numbers is a prime number, then two extraterrestrials can be friends. But every extraterrestrial can only has at most one friend. You are given all number of the extraterrestrials, please determining the maximum number of friend pair.

Input

There are several test cases.
Each test start with positive integers N(1 ≤ N ≤ 100), which means there are N extraterrestrials on the alien planet. 
The following N lines, each line contains a positive integer pi ( 2 ≤ pi ≤10^18),indicate the i-th extraterrestrial is born with pi number.
The input will finish with the end of file.

Output

For each the case, your program will output maximum number of friend pair.

Sample Input

322342538

Sample Output

12

比较裸的二分匹配,可是pi值太大,无法测试素数。

拉宾米勒测试可快速用于判断一个大数是否是素数。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<time.h>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef unsigned long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;int vis[110],lin[110];int mp[110][110],n;LL num[110];/********************//拉宾米勒测试LL MIN;LL mult_mod(LL a,LL b,LL n){    LL s=0;    while(b)    {        if(b&1) s=(s+a)%n;        a=(a+a)%n;        b>>=1;    }    return s;} LL pow_mod(LL a,LL b,LL n){    LL s=1;    while(b)    {        if(b&1) s=mult_mod(s,a,n);        a=mult_mod(a,a,n);        b>>=1;    }    return s;} bool Prime(LL n){    LL u=n-1,pre,x;    int i,j,k=0;    if(n==2||n==3||n==5||n==7||n==11)  return 1;    if(n==1||(!(n%2))||(!(n%3))||(!(n%5))||(!(n%7))||(!(n%11)))   return 0;    for(;!(u&1);k++,u>>=1);    srand((LL)time(0));    for(i=0;i<5;i++)    {        x=rand()%(n-2)+2;        x=pow_mod(x,u,n);        pre=x;        for(j=0;j<k;j++)        {            x=mult_mod(x,x,n);            if(x==1&&pre!=1&&pre!=(n-1))                return 0;            pre=x;        }        if(x!=1)  return false;    }    return true;}*************************/bool dfs(int x){    for(int i=1;i<=n;i++)    {        if(mp[x][i]&&!vis[i])        {            vis[i]=1;            if(lin[i]==-1||dfs(lin[i]))            {                lin[i]=x;                return true;            }        }    }    return false;}int main(){    while(~scanf("%d",&n))    {        CLEAR(mp,0);        REPF(i,1,n)  scanf("%lld",&num[i]);        REPF(i,1,n)        {            for(int j=i+1;j<=n;j++)            {                if(Prime(num[i]+num[j]))                    mp[i][j]=mp[j][i]=1;            }        }        int ans=0;CLEAR(lin,-1);        for(int i=1;i<=n;i++)        {            CLEAR(vis,0);            if(dfs(i)) ans++;        }        printf("%d\n",ans/2);    }    return 0;}


0 0
原创粉丝点击