Topic 3: Prime number-related time limits:2000msSingle Point time limit:1000msMemory Limit:256MB
Describe
Two numbers A and B (a<b) are referred to as prime number correlation, which refers to AXP = B, where p is a prime number. A set of S is called a prime correlation, which means that there are two prime number-related numbers in S, otherwise the s are not prime numbers. such as {2, 8, 17} prime numbers are irrelevant, but {2, 8, 16}, {3, 6} prime numbers are related. Now given a collection of s, ask the size of the largest subset of all prime number unrelated subsets of S.
Input
The first behavior is a number T, which is the number of data groups. Each group of data then contains two rows.
The first behavior n, the size of the set S. The second behavior is n integers, representing the number within the collection.
Output
For each set of data output line, the shape is "case #X: Y". X is the data number, starting with 1, Y is the size of the largest subset.
Data range
1≤t≤20
The number 22 in the set S is different and ranges from 1 to 500000.
Small Data
1≤n≤15
Big Data
1≤n≤1000
-
-
Sample input
-
-
352 4 8 16 3252 3 4 6 931 2 3
-
-
Sample output
-
-
Case #1:3Case #2:3Case #3:2
The number of mass is divided into 2 parts according to the decomposition of the elements, the binary graph matches,
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)), #define INF (2139062143) #define F (100000007) #define MAXT (20+10) #define MAXN (1000+10) #define MAXN ( 2000+10) #define MAXM (10000000) #define MAXAI (500000+10) typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void UPD (ll &a,ll b) {a= (a%f+b%f)%F;} Class Max_flow//dinic+ current arc optimization {public:int n,s,t; int Q[MAXN]; int edge[maxm],next[maxm],pre[maxn],weight[maxm],siz; void Addedge (int u,int v,int W) {edge[++siz]=v; Weight[siz]=w; Next[siz]=pre[u]; Pre[u]=siz; } void Addedge2 (int u,int v,int W) {Addedge (u,v,w), Addedge (v,u,0);} BOOL B[MAXN]; int D[MAXN]; BOOL SPFA (int s,int t) {for (i,n) D[i]=inf; MEM (b) d[q[1]=s]=0;b[s]=1; int head=1,tail=1; while (head<=tail) {int now=q[head++]; Forp (now) {int &v=edge[p]; if (Weight[p]&&!b[v]) {d[v]=d[now]+1; B[v]=1,q[++tail]=v; } } } return b[t]; } int ITER[MAXN]; int dfs (int x,int f) {if (x==t) return F; Forpiter (x) {int v=edge[p]; if (Weight[p]&&d[x]<d[v]) {int Nowflow=dfs (v,min (weight[p],f)); if (nowflow) {weight[p]-=nowflow; Weight[p^1]+=nowflow; return nowflow; }}} return 0; } int Max_flow (int s,int t) {int flow=0; while (SPFA (s,t)) {for (i,n) iter[i]=pre[i]; int F; while (F=dfs (S,inf)) flow+=f; } return flow; } void mem (int n,int s,int t) {(*this). N=n; (*this). t=t; (*this). S=s; Siz=1; MEM (PRE)}}s; INT B[maxai]={0};int p[maxai],tot=0;void make_prime (int n) {Fork (i,2,n) {if (!b[i]) p[++tot]=i; for (J,tot) {if (i*p[j]>n) break; b[i*p[j]]=b[i]+1; if (i%p[j]==0) break; }}} int n,a[maxn];int n1,n2;int A1[MAXN],A2[MAXN]; int main () {//freopen ("erfentu.in", "R", stdin); int t;cin>>t;make_prime (500000); B[1]=1;//for (i,100) cout< <i<< ': ' <<b[i]<< '; for (kcase,t) {scanf ("%d", &n); for (I,n) scanf ("%d", &a[i]); sort (a+1,a+1+n); n1=0,n2=0; for (I,n) if (b[a[i]]%2) A1[++n1]=a[i];else a2[++n2]=a[i];int s=1,t=2+n; S.mem (2+n,1,2+n); for (I,N1) s.addedge2 (s,i+1,1); for (I,N2) s.addedge2 (1+n1+i,t,1);//for (I,N1) cout<<a1[i]<< "; Cout<<endl;//for (I,N2) cout< <a2[i]<< ';cout<<endl; for (I,N1) for (j,n2) {int x=a1[i],y=a2[j];if (x==1) {if (!b[y]) S.addedge (1+i,1+n1+j,1);} Else{if ((x<y&& (y%x==0) &&!b[y/x]) | | (y<x) && (x%y==0) &amP;&!b[x/y]) S.addedge2 (1+i,1+n1+j,1);}} int Ans=0;ans=n-s.max_flow (s,t);p rintf ("Case #%d:%d\n", Kcase,ans);} return 0;}
Hiho Programming beauty 2015 first round (prime number correlation-2-point graph match)