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

CSU 1552: Friends 图论匹配+超级大素数判定

时间:2015-04-01 23:32:32      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

1552: Friends

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

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cmath>
  4 #include <algorithm>
  5 #include <string>
  6 #include <vector>
  7 #include <set>
  8 #include <map>
  9 #include <stack>
 10 #include <queue>
 11 #include <sstream>
 12 #include <iomanip>
 13 #include <ctime>
 14 using namespace std;
 15 typedef long long LL;
 16 const int INF=0x4fffffff;
 17 const int EXP=1e-5;
 18 const int MS=105;
 19 
 20 int vis[MS];
 21 int edge[MS][MS];
 22 
 23 int lin[MS];
 24 LL a[MS];
 25 int n;
 26 
 27 
 28 //
 29 LL MIN;
 30 LL mult_mod(LL a,LL b,LL n)
 31 {
 32     LL s=0;
 33     while(b)
 34     {
 35         if(b&1) s=(s+a)%n;
 36         a=(a+a)%n;
 37         b>>=1;
 38     }
 39     return s;
 40 }
 41 
 42 LL pow_mod(LL a,LL b,LL n)
 43 {
 44     LL s=1;
 45     while(b)
 46     {
 47         if(b&1) s=mult_mod(s,a,n);
 48         a=mult_mod(a,a,n);
 49         b>>=1;
 50     }
 51     return s;
 52 }
 53 
 54 bool Prime(LL n)
 55 {
 56     LL u=n-1,pre,x;
 57     int i,j,k=0;
 58     if(n==2||n==3||n==5||n==7||n==11)  return 1;
 59     if(n==1||(!(n%2))||(!(n%3))||(!(n%5))||(!(n%7))||(!(n%11)))   return 0;
 60     for(;!(u&1);k++,u>>=1);
 61     srand((LL)time(0));
 62     for(i=0;i<5;i++)
 63     {
 64         x=rand()%(n-2)+2;
 65         x=pow_mod(x,u,n);
 66         pre=x;
 67         for(j=0;j<k;j++)
 68         {
 69             x=mult_mod(x,x,n);
 70             if(x==1&&pre!=1&&pre!=(n-1))
 71                 return 0;
 72             pre=x;
 73         }
 74         if(x!=1)  return false;
 75     }
 76     return true;
 77 }
 78 
 79 bool  dfs(int x)     //   dfs增广路
 80 {
 81       for(int i=1;i<=n;i++)
 82       {
 83             if(edge[x][i]&&!vis[i])
 84             {
 85                   vis[i]=1;
 86                   if(lin[i]==-1||  dfs(lin[i]))     // i没有匹配 或匹配了可以得到增广路
 87                   {
 88                         lin[i]=x;
 89                         return true;
 90                   }
 91             }
 92       }
 93       return false;
 94 }
 95 
 96 int main()
 97 {
 98       while(scanf("%d",&n)!=EOF)
 99       {
100             memset(edge,0,sizeof(edge));
101             for(int i=1;i<=n;i++)
102                   scanf("%lld",&a[i]);
103             for(int i=1;i<=n;i++)
104                   for(int j=i+1;j<=n;j++)
105                         if(Prime(a[i]+a[j]))
106                               edge[i][j]=edge[j][i]=1;
107             int ans=0;
108             memset(lin,-1,sizeof(lin));
109             for(int i=1;i<=n;i++)
110             {
111                   memset(vis,0,sizeof(vis));
112                   if(dfs(i))
113                         ans++;
114             }
115             printf("%d\n",ans/2);
116       }
117       return 0;
118 }

 

 

CSU 1552: Friends 图论匹配+超级大素数判定

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4385454.html

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