首页 > 代码库 > CSU 1552 Friends(二分图 + 米勒测试)
CSU 1552 Friends(二分图 + 米勒测试)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552
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
Source
题意:
给你n个数,两个数相加为素数的时候,就可以成为朋友,选过的数字不能重复选择。
题解:
2分图最大匹配问题,和米勒测试。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 #define pb push_back 14 #define mp make_pair 15 #define ms(a, b) memset((a), (b), sizeof(a)) 16 //#define LOCAL 17 #define eps 0.0000001 18 #define LNF (1<<60) 19 typedef long long LL; 20 const int inf = 0x3f3f3f3f; 21 const int maxn = 100+10; 22 const int mod = 1e9+7; 23 LL a[maxn]; 24 bool Map[maxn][maxn], vis[maxn]; 25 int lin[maxn]; 26 LL big_rand(LL m) 27 { 28 LL x = rand(); 29 x*=rand(); 30 if(x<0) x-=x; 31 return x%=m; 32 } 33 LL mod_mul(LL x, LL y, LL n) 34 { 35 if(x == 0 || y == 0) return 0; 36 return (((x&1)*y)%n+(mod_mul(x>>1, y, n)<<1)%n)%n; 37 } 38 LL mod_exp(LL x, LL y, LL n) 39 { 40 LL ret = 1; 41 while(y){ 42 if(y&1) ret = mod_mul(ret, x, n); 43 x = mod_mul(x, x, n); 44 y >>= 1; 45 } 46 return ret; 47 } 48 bool Miller_Rabbin(LL n) 49 { 50 LL i, j, x, m, k; 51 if(n==2) return true; 52 if(n<2|| !(n&1)) return false; 53 m = n - 1;k = 0; 54 while(!(m&1)) m >>= 1, k++; 55 for(i=0;i<4;i++){ 56 x = big_rand(n-2) + 2; 57 x = mod_exp(x, m, n); 58 if(x == 1) continue; 59 for(j = 0;j<k;j++){ 60 if(x==n-1) break; 61 x = mod_mul(x, x, n); 62 } 63 if(j>=k) return false; 64 } 65 return true; 66 } 67 bool dfs(int x, int n){ 68 for(int j = 1;j<=n;j++){ 69 if(Map[x][j]&&!vis[j]){ 70 vis[j] = 1; 71 if(lin[j]==0 || dfs(lin[j], n)){ 72 lin[j] = x; 73 return 1; 74 } 75 } 76 } 77 return 0; 78 } 79 int main() 80 { 81 #ifdef LOCAL 82 freopen("input.txt", "r", stdin); 83 // freopen("output.txt", "w", stdout); 84 #endif // LOCAL 85 86 int n; 87 while(~scanf("%d", &n)){ 88 ms(Map, 0); 89 for(int i=1;i<=n;i++) scanf("%lld", &a[i]); 90 for(int i=1;i+1<=n;i++){ 91 for(int j=i+1;j<=n;j++){ 92 if(Miller_Rabbin(a[i]+a[j])){ 93 Map[i][j] = Map[j][i] = 1; 94 } 95 } 96 } 97 int ans = 0; 98 ms(lin, 0); 99 for(int i=1;i<=n;i++){ 100 ms(vis, 0); 101 if(dfs(i, n)) ans++; 102 } 103 printf("%d\n", ans/2); 104 } 105 return 0; 106 }
将出2分图讲解,和米勒测试。未完待续。。XD
CSU 1552 Friends(二分图 + 米勒测试)