Source: Light OJ 1356 prime independence
Question: select the largest number for N numbers to form a set so that the number of any two is not the prime number of another number X! = K * y
Ideas: the two sides of the conflict are connected, and all the numbers are divided into two parts: the odd and even numbers. The two parts are constructed based on the parity of the quality factor.
To determine the contradiction, we first break down each number factor. For example, X has A1 P1 quality factor A2 P2 quality factor... the number of prime factors of an PN prime factor X is A1 + A2 +... + an is recorded as sum.
Determine whether x/P1 x/P2... x/P3 exists and their prime factor number sum parity Different Edge HK find the largest independent set
# Include <cstdio> # include <cstring> # include <queue> # include <cmath> # include <algorithm> using namespace STD; const int maxn = 500010; const int maxm = 40010; const int INF = 999999999; struct edge {int V, next;} e [maxn]; bool vis [maxn]; int prime [maxm]; int A [maxm]; int B [maxn]; int P [maxm]; int num [maxm]; int first [maxm], CNT; int CX [maxm], CY [maxm]; int DX [maxm], Dy [maxm]; int N, DIS; void sieve (int n) {int M = sqr T (n + 0.5); memset (VIS, 0, sizeof (VIS); vis [0] = vis [1] = 1; for (INT I = 2; I <= m; I ++) if (! Vis [I]) for (Int J = I * I; j <= N; j + = I) vis [J] = 1;} int get_primes (INT N) {sieve (n); int C = 0; For (INT I = 2; I <= N; I ++) if (! Vis [I]) prime [C ++] = I; return C;} void addedge (INT U, int v) {e [CNT]. V = V; E [CNT]. next = first [u]; first [u] = CNT ++;} bool search () {Dis = inf; memset (dx,-1, sizeof (dx )); memset (dy,-1, sizeof (dy); queue <int> q; For (INT I = 1; I <= N; I ++) {If (CX [I] =-1 & (Num [I] & 1) {q. push (I); DX [I] = 0 ;}} while (! Q. empty () {int u = Q. front (); q. pop (); If (dx [u]> dis) break; For (INT I = first [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (dy [v] =-1) {dy [v] = DX [u] + 1; if (CY [v] =-1) {Dis = Dy [v];} else {DX [CY [v] = Dy [v] + 1; q. push (CY [v]) ;}}} return dis! = Inf;} bool DFS (int u) {for (INT I = first [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (dy [v] = DX [u] + 1) {dy [v] = 0; if (CY [v] =-1 | DFS (CY [v]) {cx [u] = V; CY [v] = u; // printf ("** % d \ n", u, v); Return true ;}}return false ;}int match () {int ans = 0; memset (CX,-1, sizeof (CX); memset (CY,-1, sizeof (CY); While (search () {for (INT I = 1; I <= N; I ++) {If (CX [I] =-1 & (Num [I] & 1) & DFS (I )) ans ++ ;}}return ans ;}int main () {int CAS = 1; int t; scanf ("% d", & T ); int c = get_primes (500000); While (t --) {scanf ("% d", & N); memset (B, 0, sizeof (B )); memset (Num, 0, sizeof (Num); memset (first,-1, sizeof (first); CNT = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]);} Sort (a + 1, A + n + 1 ); for (INT I = 1; I <= N; I ++) {B [A [I] = I;} For (INT I = 1; I <= N; I ++) {int x = A [I]; int sum = 0; int sum2 = 0; For (Int J = 0; j <C & prime [J] * prime [J] <= x; j ++) {If (X % prime [J] = 0) {P [Sum ++] = prime [J]; while (X % prime [J] = 0) {x/= prime [J]; sum2 ++ ;}}if (x> 1) {P [Sum ++] = x; sum2 ++;} num [I] = sum2; // printf ("*** % d \ n", sum2); For (Int J = 0; j <sum; j ++) {int temp = B [A [I]/P [J]; If (temp> I) continue; If (temp) {If (sum2 & 1) = (Num [temp] & 1) continue; // printf ("% d \ n", I, temp); If (sum2 & 1 )) addedge (I, temp); elseaddedge (temp, I) ;}} int ans = match (); printf ("case % d: % d \ n ", CAS ++, N-ans);} return 0 ;}