Mike and GCD Issues

4.4

38 votes
Ad-Hoc, Approved, Easy, Greatest common divisor, Math, Number theory, gcd
Problem

Mike claims to be good at math, and a person having a photographic memory. Harvey does trust Mike, but he is not completely sure about these claims said by him. So, for reassurance, he gives the following task to Mike :

Given a 1-indexed array A of size N, the distance between any 2 indices of this array i and j is given by |ij|. Now, given this information, Mike needs to find for every index i (1iN), an index j, such that 1jN, ij, and GCD(A[i],A[j])>1

If there are multiple such candidates for an index i, you need to find and print the index j, such that the distance between i and j is minimal. If there still exist multiple candidates, print the minimum j satisfying the above constraints.

For each index i of this array, find and print an index j satisfying the conditions above. If, for any index i there does not exist any j, print 1 instead of its answer.

Input Format:

The first line contains a single integer N denoting the size of array A. The next line contains N space separated inetegers, where the ith integer denotes A[i].

Output Format :

Print N space separated integers, where the ith integer denotes an index j, where 1jN, ij, and GCD(A[i],A[j])>1, and the distance between i and j is minimal if there exist multiple candidates satisfying the above constraints. If there still exist multiple candidates satisfying the above constraints, print the minimum j doing so.

Constraints :

1N2×105

1A[i]2×105

Sample Input
5
2 3 4 9 17
Sample Output
3 4 1 2 -1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The closest index to index 1 satisfying the above constraints is index 3, as GCD(2,4)=2, that is greater than one. Similarily, the closest indices to indices 2,3, and 4 are indices 4,1 and 2 respectively.

Editor Image

?