1552:friends time limit: 3 Sec Memory Limit:
Submit: 187 Solved: 43
[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
Hint when a matching problem is encountered and can only be used once, consider the two-point maximum match.
#include <stdio.h> #include <math.h> #include <algorithm> #include <string.h> #include < time.h>using namespace std; #define LL Long long//-----Large prime number judgment-------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); }}
Csu1552:friends (quickly determine if large numbers are not prime + binary match)