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 are born with a number. If the sum of the numbers is a prime number and then both extraterrestrials can be friends. But every extraterrestrial can only have at the most one friend. You is given all number of the extraterrestrials and please determining the maximum number of friend pair.
Input
There is several test cases.
Each test start with positive integers n (1≤n≤100), which means there is 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 Bor N with pi number.
The input would finish with the end of file.
Output
For each case, your program would output maximum number of friend pair.
Sample Input
322342538
Sample Output
12
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>Ten#include <queue> One#include <sstream> A#include <iomanip> -#include <ctime> - using namespacestd; thetypedefLong LongLL; - Const intinf=0x4fffffff; - Const intexp=1e-5; - Const intms= the; + - intVis[ms]; + intEdge[ms][ms]; A at intLin[ms]; - LL A[ms]; - intN; - - - // in LL MIN; - ll Mult_mod (ll a,ll b,ll N) to { +LL s=0; - while(b) the { * if(b&1) s= (s+a)%N; $A= (a+a)%N;Panax Notoginsengb>>=1; - } the returns; + } A the 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); theb>>=1; - }Wuyi returns; the } - Wu BOOLPrime (LL N) - { AboutLL u=n-1, pre,x; $ inti,j,k=0; - if(n==2|| n==3|| n==5|| n==7|| n== One)return 1; - if(n==1|| (! (n%2))|| (! (n%3))|| (! (n%5))|| (! (n%7))|| (! (n% One)))return 0; - for(;! (u&1); k++,u>>=1); ASrand (LL) Time (0)); + for(i=0;i<5; i++) the { -X=rand ()% (n2)+2; $x=Pow_mod (x,u,n); thePre=x; the for(j=0; j<k;j++) the { thex=Mult_mod (x,x,n); - if(x==1&&pre!=1&&pre!= (n1)) in return 0; thePre=x; the } About if(x!=1)return false; the } the return true; the } + - BOOLDfsintX//Dfs Augmentation Road the {Bayi for(intI=1; i<=n;i++) the { the if(edge[x][i]&&!Vis[i]) - { -vis[i]=1; the if(lin[i]==-1|| DFS (Lin[i]))//I have no matching or matching can get the augmented path the { thelin[i]=x; the return true; - } the } the } the return false;94 } the the intMain () the {98 while(SCANF ("%d", &n)! =EOF) About { -Memset (Edge,0,sizeof(Edge));101 for(intI=1; i<=n;i++)102scanf"%lld",&a[i]);103 for(intI=1; i<=n;i++)104 for(intj=i+1; j<=n;j++) the if(Prime (a[i]+A[j]))106edge[i][j]=edge[j][i]=1;107 intcn1=0;108memset (lin,-1,sizeof(Lin));109 for(intI=1; i<=n;i++) the {111memset (Vis,0,sizeof(Vis)); the if(Dfs (i))113ans++; the } theprintf"%d\n", ans/2); the }117 return 0;118}
CSU 1552:friends graph theory match + super large prime number judgment