Non-decreasing arrays

3.6

65 votes
Arrays, C++, 1-D, Data Structures, Greedy Algorithms, Basics of Greedy Algorithms, Math
Problem

You are given an array A consisting of N positive integers. Your task is to find an array B of length N satisfying the following conditions:

  • Bi>0 for all 1iN
  • BiBi+1, for all 1i<N
  • Bi is divisible by Ai for all 1iN
  • Ni=1Bi is minimum

You are given T test cases.

Input format

  • The first line contains a single integer T denoting the number of test cases.
  • The first line of each test case contains a single integer N denoting the length of the array.
  • The second line of each test case contains N space-separated integers denoting the integer array A.

Output format

For each test case (in a separate line), print N space-separated integers denoting B1,B2,..,BN. If there are multiple answers, you can print any of them. It is guaranteed that under the given constraints at least 1 B exists.

Constraints

1T10001N2.5×1051Ai109Sum of N over all test cases does not exceed 7.5×105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Self explanatory.

Editor Image

?