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;
}
 





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值