CSU 1552: Friends(素数测试+二分匹配)
来源:互联网 发布:最优化 中科院 编辑:程序博客网 时间:2024/04/24 18:38
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
322342538
Sample Output
12
比较裸的二分匹配,可是pi值太大,无法测试素数。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<time.h>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef unsigned long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;int vis[110],lin[110];int mp[110][110],n;LL num[110];/********************//拉宾米勒测试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;}*************************/bool dfs(int x){ for(int i=1;i<=n;i++) { if(mp[x][i]&&!vis[i]) { vis[i]=1; if(lin[i]==-1||dfs(lin[i])) { lin[i]=x; return true; } } } return false;}int main(){ while(~scanf("%d",&n)) { CLEAR(mp,0); REPF(i,1,n) scanf("%lld",&num[i]); REPF(i,1,n) { for(int j=i+1;j<=n;j++) { if(Prime(num[i]+num[j])) mp[i][j]=mp[j][i]=1; } } int ans=0;CLEAR(lin,-1); for(int i=1;i<=n;i++) { CLEAR(vis,0); if(dfs(i)) ans++; } printf("%d\n",ans/2); } return 0;}
- CSU 1552: Friends(素数测试+二分匹配)
- CSU 1552 Friends(Miller_Rabin素数测试+二分图匹配)
- CSU 1552-Friends(大数判断素数+二分匹配)
- csu 1552: Friends(拉宾米勒测试+二分匹配)
- csu 1552: Friends(大素数判断+二分图)
- CSU1552: Friends(快速判断大数是不是素数+二分匹配)
- CSU 1552( MiLLer_Rabin+二分图匹配)
- CSU 1030 素数槽(素数打表+二分)
- CSU 1531 Jewelry Exhibition 二分匹配(裸
- 素数伴侣--最大二分匹配
- 牛客网 素数伴侣(二分图最大匹配,匈牙利算法)
- CSU 1623 Inspectors(二分图最大权匹配 KM算法)(UVAlive 6879)
- CSU-1531 Jewelry Exhibition —— 二分图匹配(最小覆盖点)
- hdu 5943 素数分布+二分图匹配
- 最大二分匹配:最大“素数伴侣”问题
- csu(欧拉函数+筛素数)
- xdu_1048:二分匹配模板测试
- CSU 1685-Entertainment Box(set+二分)
- c++获取系统信息(操作系统,cpu,内存,ip,mac)
- const浅析
- shell script简单范例
- iOS判断NSString 只含有数字
- 获取月份天数
- CSU 1552: Friends(素数测试+二分匹配)
- ThreadLocal
- 移动平台的meta标签-----神奇的功效
- Discuz X3.1微信打通版,发布回复时出现提示:您的请求来路不正确或表单验证串不符,无法提交问题的解决
- Android系统自带录屏(动态截屏)功能
- Apache运行机制剖析
- pl/sql导出dmp
- cocos2dx 3.4 lua加密 setXXTEAKeyAndSign
- 简单编程 (二十七)创建窗体下拉列表