码迷,mamicode.com
首页 > 其他好文 > 详细

csu 1552: Friends(大素数判断+二分图)

时间:2015-05-18 09:05:00      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:csu 1552 friends   大素数判断   二分图   

1552: Friends

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

HINT


题意:给你n个人,每个人有一个值,然后交朋友,一个人只能有一个朋友,两个人可以交朋友的条件是两个人的值的和是素数。由于值很大,所以要用用随机测试。


题解:二分图,每两个人的值得和是素数的话就建一条边,双向。然后求二分最大匹配。


code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<time.h>
#include<cstdlib>
#define Mod 1000000007
#define ll long long
#define N 110
#define INF 1010010010

using namespace std;

ll a[N];
int n,k;

int linker[N];
bool used[N];
int c[N];
int maze[N][N];

const int S=8;

ll mult_mod(ll a,ll b,ll c) {
    a%=c;
    b%=c;
    ll ret=0;
    while(b) {
        if(b&1) {
            ret+=a;
            if(ret>c)ret-=c;
        }
        a<<=1;
        if(a>=c)a-=c;
        b>>=1;
    }
    return ret;
}

ll pow_mod(ll x,ll n,ll mod) { //x^n%c
    if(n==1)return x%mod;
    x%=mod;
    ll tmp=x;
    ll ret=1;
    while(n) {
        if(n&1) ret=mult_mod(ret,tmp,mod);
        tmp=mult_mod(tmp,tmp,mod);
        n>>=1;
    }
    return ret;
}

bool check(ll a,ll n,ll x,ll t) {
    ll ret=pow_mod(a,x,n);
    ll last=ret;
    for(int i=1; i<=t; i++) {
        ret=mult_mod(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1) return true;//合数
        last=ret;
    }
    if(ret!=1) return true;
    return false;
}

///大素数判断
bool Miller_Rabin(ll n) {
    if(n<2)return false;
    if(n==2)return true;
    if((n&1)==0) return false;
    ll x=n-1;
    ll t=0;
    while((x&1)==0) {
        x>>=1;
        t++;
    }
    for(int i=0; i<S; i++) {
        ll a=rand()%(n-1)+1;
        if(check(a,n,x,t))
            return false;
    }
    return true;
}

bool dfs(int u) {
    int v;
    for(v=1; v<=n; v++)
        if(maze[u][v]&&!used[v]) {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v])) {
                linker[v]=u;
                return true;
            }
        }
    return false;
}

int hungary() {
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1; u<=n; u++) {
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    return res;
}

int main() {
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n)) {
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        memset(maze,0,sizeof maze);
        for(int i=1; i<=n; i++) {
            for(int j=i+1; j<=n; j++) {
                if( Miller_Rabin(a[i]+a[j])) {
                    maze[i][j]=maze[j][i]=1;
                }
            }
        }
        printf("%d\n",hungary()/2);
    }
    return 0;
}


csu 1552: Friends(大素数判断+二分图)

标签:csu 1552 friends   大素数判断   二分图   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45799557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!