CSU1552: Friends(快速判断大数是不是素数+二分匹配)
2015-04-04 13:54
435 查看
1552: Friends
Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 187 Solved: 43
[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
当遇到匹配问题时,而且只能用一次,这时应想一下二分最大匹配。#include<stdio.h> #include<math.h> #include <algorithm> #include<string.h> #include <time.h> using namespace std; #define ll long long //-----大素数的判断------- 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,ok[105][105],mark[105],vist[105]; int DFS(int x) { for(int i=1;i<=n;i++) if(ok[x][i]&&vist[i]==0) { vist[i]=1; if(mark[i]==0||DFS(mark[i])) { mark[i]=x; return 1; } } return 0; } int main() { ll a[105]; while(scanf("%d",&n)>0) { for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); } memset(ok,0,sizeof(ok)); for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++) if(Prime(a[i]+a[j])) ok[i][j]=ok[j][i]=1; int ans=0; memset(mark,0,sizeof(mark)); for(int i=1;i<=n;i++) { memset(vist,0,sizeof(vist)); ans+=DFS(i); } printf("%d\n",ans/2); } }
相关文章推荐
- CSU 1552-Friends(大数判断素数+二分匹配)
- CSU 1552: Friends(素数测试+二分匹配)
- csu 1552: Friends(拉宾米勒测试+二分匹配)
- CSU 1552: Friends 图论匹配+超级大素数判定
- csu 1552: Friends(大素数判断+二分图)
- 判断素数+二分快速求幂——pku3641
- 匈牙利最大匹配+大质数判定 csu1552 Friends
- 大数是否为素数【费马小定理+Carmichael数判断】
- 判断很大的数是不是素数(模版)
- 判断一个数是不是素数的方法
- 通过一个函数,判断输入的数是不是素数
- HDU 2444 The Accomodation of Students(最大二分匹配(匈牙利算法)+二分图判断->模板题目)
- 判断一个数是不是素数
- 看人家小学生的素数的快速判断
- C判断一个数是不是素数
- 判断大数是否是素数问题
- C判断一个数是不是素数
- HLG 1238 Nova【二分+二分图多重匹配+圆与线段相交判断】
- 通过一个函数,判断输入的数是不是素数
- C语言中素数的快速判断和筛法创建。