### PROBLEM LINK:

**Author:** Lalit Kundu

**Tester:** Shang Jingbo and Gerald Agapov

**Editorialist:** Devendra Agarwal

### DIFFICULTY:

HARD

### PREREQUISITES:

Max-Flow and Maths.

### PROBLEM:

You are given two arrays denoted by A_{1},A_{2}…A_{N} and B_{1},B_{2}…B_{N}.

Let us maintain two sets S_{1} and S_{2} which are empty initially. In one move , pick a pair of indexes (i, j) such that it’s already not present in S_{1}. Also, B_{j} > A_{i} and GCD(A_{i},B_{j}) is not 1. Further,pick another pair of indices (p, q) such that it’s already not present in S_{2}. Also, B_{p} < A_{q} and GCD(A_{q},B_{p}) is not 1. Also, GCD(A_{q},B_{p}) should not be coprime to GCD(A_{i},B_{j}). Then add both pair of indices to S_{1} and S_{2}, respectively.

Find the largest number of moves that can be performed?

### Explanation

Let us first find (i,j) pairs which satisfy the property that they are unique [ i.e. (i,j) is considered once ] and B_{j} > A_{i} and GCD(A_{i},B_{j}) is not 1. Let us store gcd of each pair in vector U.

Let us now find (p,q) pairs which satisfy the property that they are unique [ i.e. (p,q) is considered once ] and B_{p} < A_{q} and GCD(A_{q},B_{p}) is not 1. Let us store gcd of each pair in vector V.

You would have realized that we are not interested in the pair (i,j) but we are interested in their gcd.

So , we are given 2 arrays U and V , we need to find pairs ( U[x] , V[y] ) , such that GCD ( U[x] , V[y] ) is not 1 and x is choosen atmost once and similarly y is choosen atmost once. Our target is to maximise such pairs.

The above stated problem is indeed we need to solve. The condition “x is choosen atmost once” ensures that pair (i,j) is not repeated , and similarly for y. The condition “GCD ( U[x] , V[y] ) is not 1” ensures " GCD(A_{q},B_{p}) should not be coprime to GCD(A_{i},B_{j})" is fulfilled.

This is a classical max-flow problem.Consider the nodes as (U + V + source + sink) and consider adding edges from source to each index of U with capacity 1 ,add edges from x to y if gcd of U[x] and V[y] is not 1 , and edges from each index of V to sink with capacity 1.

Calculating max-flow of this graph will give us the solution.

**But Wait we are not done with the solution!**

The Above solution is sure to get time out.

**Reason**

Size of the vector U in the worst case will be 400 * 400 , and the size of the vector V in worst case will be 400 * 400. So total number of edges between U and V can go upto 400 * 400 * 400 * 400 , which is too high.

I will urge readers to read the problem statement once again and make a small observation which will be the key to solve the problem.

Can we do some observation on the line “add an edge from x to y if gcd of U[x] and V[y] is not 1” ? .

Our only aim is to ensure that the gcd is not 1 , so we can introduce some dummy vertex , which will denote prime factor of some number from the array U or V.

What will be the bound on such vertices ?

Such vertices are bounded by 9* ( sizeof(U) + sizeof(V) ). You can quickly realise that the maximum prime factor a number <= 1e9 is 9 ( **Reason** : 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 > 1e9 ).

How will one use such vertices to fullfill the aim of the problem ?

Let’s consider the graph ( Nodes , Edges ) where Nodes = U + V + source + sink + dummy vertices , and Edges = E_{1} + E_{2} + E_{3} + E_{4}

E_{1} : Edges from source to each index of U with capacity 1 [ same as described above ]

E_{2} : Edges from each index of V to sink with capacity 1 [ same as described above ]

E_{3} : Edges from index x of vector U to those dummy vertices which divides U[x] with Infinite capacity.

E_{4} : Edges from those dummy vertices to index x of vector V , which dividex V[x] with Infinite capacity .

Number of Edges in this graph <= 400 * 400 + 9 * 400 * 400 + 9 * 400 * 400 + 400 * 400

Number of Nodes in this graph <= 1 + 400 * 400 + 9 * 400 * 400 + 9 * 400 * 400 + 400 * 400 + 1

You need to proof that the max-flow in this graph is indeed same as the max-flow in the previous graph.