Time limit:2000msSingle Point time limit:1000msMemory Limit:256MB
description
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
2. Problem-Solving ideas: The problem with the maximum flow solution. You can first find out all the number-related numbers, and use the SS array to mark them. The next step is the process of building the map. If the number a[i] is prime-related, connect an edge between I and the source point S. Otherwise, connect it to the meeting point T with an edge. Next, if the number a[i]=a[j]*p, or a[j]=a[i]*p, is found, then an edge is connected between I and J. All sides have a capacity of 1. Next, solve the maximum flow for this graph. Using the minimum-cut maximum flow theorem, it is known that N-maxflow () is a subset of the most unrelated points containing prime numbers.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace Std;int ss[510000], T, A[1100];bool prime[ 510000]; #define INF 1000000000#define min (x, y) ((x) < (y)? (x): (y)) int n, m, I, Q, H, Mid, Len, S, T, till[3100000], go[3100000], next[3100000], f[3100000], d[3100000], n1[3100000 ];bool cc[3100000];void Add (int x, int y, int z) {Next[++len] = till[x];till[x] = Len;go[len] = Y;f[len] = z;} void Ad (int x, int y, int z) {Add (x, y, z); Add (y, x, 0);} BOOL BFs () {int q, h, I;memset (d, 0, sizeof D), memset (cc, 1, sizeof cc); for (d[n1[q = h = 1] = s] = 1; q <= h; q++) for (i = till[n1[q]]; i; i = Next[i]) if (F[i] &&! D[go[i]]) d[n1[++h] = Go[i]= D[n1[q]] + 1;return d[t];} int dfs (int k, int mi) {if (k = = t) return mi;int I, tmp, sum = 0;for (i = till[k]; i && mi; i = next[i]) if (F[i] & amp;& D[go[i] [= d[k] + 1 && cc[go[i]]) {tmp = DFS (go[i], min (mi, f[i]), sum + = tmp;mi-= tmp;f[i]-= Tmp;f[i ^ 1] + = tmp;} if (!sum) cc[k] = False;return sum;} int Maxflow () {int sum = 0;while (BFS ()) sum + = DFS (s, INF); return sum;} int main () {freopen ("T.txt", "R", stdin); ss[1] = 0;for (int i = 2; I <= 500000; i++)//sieve primes prime[i] = true;for (int i = 2 ; I <= 500000; i++) {for (int j = i + i; j <= 500000; j + = i) prime[j] = false;} for (int i = 1, i <= 500000; i++) for (int j = 1; J <= 500000/i; j + +) if (Prime[j]) ss[i * j] = 1-ss[i];//All prime-number related The number is marked as 1SCANF ("%d", &t), for (int mm = 1; mm <= T; mm++) {printf ("Case #%d:", mm), scanf ("%d", &n), and for (int i = 1; I <= N; i++) scanf ("%d", &a[i]), len = 1;s = 0;//Source Point t = n + 1;//sink point for (int i = 0; I <= n + 1; i++) till[i] = 0;for (int i = 1; I <= N; i++) if (ss[A[i]) Ad (0, I, 1);//If a[i] is the number of prime numbers related, the source point and I connect an edge else Ad (i, n + 1, 1);//Otherwise, I and the sink point are connected for (int i = 1; I <= n; i++) if (Ss[a[i]]) fo R (Int j = 1; J <= N; j + +) if (!ss[a[j]]) {if (A[i] > A[j]) {if (A[i]% a[j] = = 0 && prime[a[i]/a[j]) Ad (i , j, 1);//If a[i] is a prime number-related and that prime number is a[i]/a[j], connect an edge between I and J}else {if (A[j]% a[i] = = 0 && prime[a[j]/a[i]) Ad (i, J, 1);}} printf ("%d\n", N-maxflow ());//Dinic algorithm to find the maximum flow, using the minimum cut maximum flow theorem, the remaining point is the largest subset}}
2015 Programming beauty preliminary first field C prime number correlation