NEED HELP IN ARRGRAPH

Oct 19, 2018 at 8:13pm
You are given a sequence of integers A1,A2,…,AN. You may change any number of its elements (possibly zero), obtaining a new sequence of positive integers B1,B2,…,BN. Each element of B must be an integer between 2 and 50 (both inclusive).

Let's define an undirected graph G with N vertices (numbered 1 through N). For each pair of different vertices i and j, there is an edge between i and j if and only if Bi and Bj are coprime.

You should choose the sequence B in such a way that G is a connected graph. The number of elements which need to be changed to obtain B from A should be minimum possible. Find one such sequence B and the minimum required number of changes.

It can be proven that a solution always exists — it is always possible to modify sequence A in such a way that G is connected.

Example Input
2
2
2 3
2
2 4
Example Output
0
2 3
1
2 3
Explanation
Example 1: There is an edge in G between vertices 1 and 2. This graph is connected, so there is no need to change any elements.

Example 2: There is no edge between vertices 1 and 2 since gcd(2,4)≠1. This graph is not connected. We can change element A2=4 to 3 and make this graph connected.
Oct 19, 2018 at 8:13pm
can anyone help ? please give me some idea to solve
Oct 19, 2018 at 10:43pm
use below code just after main() in your code
1
2
ios_base::sync_with_stdio(false);
cin.tie(NULL);

Oct 19, 2018 at 11:01pm
No, till now i'm not getting any appropriate logic.
Oct 19, 2018 at 11:02pm
do one thing store all prime number upto 50 before test case and then submit
Oct 20, 2018 at 1:15pm
first mistake is print each output in seperate line
on input
1
2
5
2 4 3 7 3

output should be
1
2
0
2 4 3 7 3
Oct 20, 2018 at 6:48pm
anyone can give some hint or logic to solve PERIODIC question
Topic archived. No new replies allowed.