• 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
    ",ans/2);
        }
        return 0;
    }
  • 相关阅读:
    java web前端发送请求的4种方式
    简单的jQuery前端验证码校验
    验证码实现原理
    Objective-C的内存管理(一)黄金法则的理解
    UIview层次管理
    IOS设置图片背景
    Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
    Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
    Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
    UVA 10209
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5793156.html
Copyright © 2020-2023  润新知