天天看點

csu 1552: Friends(拉賓米勒測試+二分比對)

1552: Friends

Time Limit: 3 Sec   Memory Limit: 256 MB

Submit: 129   Solved: 24

[ Submit][ Status][ Web Board]

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

3
2
2
3

4
2
5
3
8      

Sample Output

1
2      

題意很簡單。。。

主要的是大數判斷是否是質數  這裡用到的是拉賓米勒測試 不懂的請百度

另外 在二分圖比對時  要建無向圖。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <cstdlib>
#include <time.h>
#define ll long long
 
using namespace std;
 
ll MIN;
//ll gcd(ll a,ll b)
//{
//    if(b==0) return a;
//    else return (b,a%b);
//}
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;
}
 
int 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 0;
    }
    return 1;
}
 
int N;
int mp[110][110];
int linker[110];
int vis[110];
int ans;
 
bool dfs(int u)
{
    for(int i=0;i<N;i++)
    {
        if(mp[u][i]&&!vis[i])
        {
            vis[i]=1;
            if(linker[i]==-1||dfs(linker[i]))
            {
                linker[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
 
void hungary()
{
    memset(linker,-1,sizeof(linker));
    for(int i=0;i<N;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
        {
            ans++;
//            cout<<"ans  "<<ans<<endl;
        }
    }
}
 
ll A[110];
 
int main()
{
//    freopen("F.txt","r",stdin);
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=0;i<N;i++)
        {
            scanf("%lld",&A[i]);
        }
        memset(mp,0,sizeof(mp));
        for(int i=0;i<N;i++)
            for(int j=i+1;j<N;j++)
            {
//                if(i==j)  continue;
                if(Prime(A[i]+A[j]))
                    mp[i][j]= mp[j][i] =1;
            }
//            for(int j=i+1;j<N;j++)
//                if(Miller_Rabin(a[i]+a[j]))
//                {
//                    mp[i][j]=mp[j][i]=1;
//                    cout<<i<<"  "<<j<<"  "<<mp[i][j]<<endl;
//                }
        ans=0;
        hungary();
        printf("%d\n",ans/2);
    }
    return 0;
}
 
           

繼續閱讀