csu 1552(米勒拉宾素数测试+二分图匹配)

1552: Friends

Time Limit: 3 Sec  Memory Limit: 256 MB
Submit: 723  Solved: 198
[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

题意:有一些外星人想要找朋友玩,每个外星人都有一个value,当另一个外星人的 value' + value 是素数时,他们就可以成为朋友,但是每个人只能有一个朋友,问最多能够有多少朋友?
题解米勒拉宾大素数判断+二分图匹配
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 505
typedef long long LL;
//拉宾米勒测试
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;
}
int n;
int graph[N][N];
int linker[N];
bool vis[N];
LL a[N];
bool dfs(int u){
    for(int i=1;i<=n;i++){
        if(graph[u][i]&&!vis[i]){
            vis[i] = true;
            if(linker[i]==-1||dfs(linker[i])){
                linker[i] = u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) graph[i][j] = 0;
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(Prime(a[i]+a[j])){
                    graph[i][j] = graph[j][i] = 1;
                }
            }
        }
        int ans = 0;
        memset(linker,-1,sizeof(linker));
        for(int i=1;i<=n;i++){
            memset(vis,false,sizeof(vis));
            if(dfs(i)) ans++;
        }
        printf("%d\n",ans/2);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5793156.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值