luogu#P16392. [IATI 2024] Lex_gcd

[IATI 2024] Lex_gcd

Problem Description

Your task is to find the lexicographically lowest KK-gcd equivalent permutation\textbf{permutation} of a given sequence of NN positive integers a1,a2,,aNa_1, a_2, \ldots, a_N. Two sequences (that are permutations of each other) a1,a2,,aNa_1, a_2, \ldots, a_N and b1,b2,,bNb_1, b_2, \ldots, b_N are considered KK-gcd equivalent if for every set of KK distinct indices from 11 to NN, the greatest common divisor (gcd) of the elements in these positions in both sequences is the same.

However, there's a twist - you are allowed to multiply at most one element of aa by a given integer XX before finding this lowest KK-gcd equivalent sequence. You are allowed to not multiply by XX at all and keep the same sequence. Additionally, it is guaranteed that X is divisible by only 1 and itself\textbf{X is divisible by only 1 and itself}. You aim to minimize the resulting sequence lexicographically among all possible choices of preprocessing (or choosing not to) of aa.

Write a program lex_gcd\texttt{lex\_gcd} that solves this problem.

Input Format

The first line of the input contains one integer TT, the number of test cases. Each test case consists of three positive integers NN, KK, and XX, followed by NN positive integers a1,a2,,aNa_1, a_2, \ldots, a_N.

Output Format

For each test case, output NN integers representing the lexicographically lowest KK-gcd equivalent sequence to aa after performing the allowed preprocessing.

2
3 2 1
2 6 4
4 2 3
7 3 6 9
2 4 6
3 6 9 21

Hint

Explanation

Two test cases. For the first one, no preprocessing is required. The KK-gcd property is satisfied since the greatest common divisor of all pairs remains 2. For the second one, preprocessing by multiplying the first element by 33 results in the lexicographically lowest sequence 3,6,9,213, 6, 9, 21.

Constraints

  • 2N1052 \leq \sum_{}{} N \leq 10^5 (over all test cases)
  • 2KN2 \leq K \leq N
  • 1X1091 \leq X \leq 10^9, XX is either 11 or prime
  • 1ai1091 \leq a_i \leq 10^9

Subtasks

Subtask Points Required subtasks NN XX Other constraints
11 66 - N5\sum N \leq 5 X=1X = 1 -
22 1919 11 N1000\sum N \leq 1000
33 1313 121-2 N105\sum N \leq 10^5
44 44 11 N100\sum N \leq 100 -
55 1,2,41,2,4 N1000\sum N \leq 1000
66 3131 - N105\sum N \leq 10^5 aiaja_i \neq a_j for iji \neq j
77 2323 161-6 -

The points for a subtask are given only if all tests for it and the required subtasks are passed successfully.