LEX_GCD
Time limit1.5sMemory limit1024 MB
Find the lexicographically smallest permutation that is K-gcd equivalent to a sequence, after optionally multiplying one element by a prime X.
- Level
Hard8 of 10
- Topics
- Number theory, Greedy, Sorting
- Solved
- No attempts yet
Problem
Your task is to find the lexicographically lowest -gcd equivalent permutation of a given sequence of positive integers . Two sequences that are permutations of each other, and , are -gcd equivalent if, for every set of distinct indices from to , the gcd of the elements at those positions is the same in both sequences.
You may multiply at most one element of by a given integer before searching. You may also leave unchanged. is either or a prime, so its only divisors are and . If you multiply an element, the result must be a -gcd equivalent of the modified , with the gcds computed after the multiplication.
Input
The first line contains one integer , the number of test cases. Each test case consists of three positive integers , , and , followed by positive integers .
Output
For each test case, print integers: the lexicographically lowest -gcd equivalent sequence of after the optional preprocessing.
Constraints
- (over all test cases)
- , and is either or prime