LEX_GCD

시간 제한1.5초메모리 제한1024 MB

요약
임의의 K개 원소 gcd를 모두 보존하는 순열 중 사전순으로 가장 작은 것을 찾되, 원소 하나에 소수 X를 곱하거나 곱하지 않을 수 있다.
난이도

어려움10점 중 8점

유형
정수론, 수학, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

Your task is to find the lexicographically lowest KK-gcd equivalent permutation of a given sequence of NN positive integers a_1,a_2,…,a_Na\_1 , a\_2 , \dots , a\_N. Two sequences (that are permutations of each other) a_1,a_2,…,a_Na\_1 , a\_2 , \dots , a\_N and b_1,b_2,…,b_Nb\_1 , b\_2 , \dots , 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 XX is divisible by only 11 and itself. You aim to minimize the resulting sequence lexicographically among all possible choices of preprocessing (or choosing not to) of aa. Note that if you decide to preprocess the sequence, then your result must be a KK-gcd equivalent of the preprocessed sequence aa (i.e. considering the greatest common divisors after the multiplication).

Write a program lex_gcd that solves this problem.

입력

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 a_1,a_2,…,a_Na\_1 , a\_2 , \dots , a\_N.

출력

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

제한

  • 2≤∑N≤1052 ≤ \sum{N} ≤ 10^5 (over all test cases)
  • 2≤K≤N2 ≤ K ≤ N
  • 1≤X≤1091 ≤ X ≤ 10^9, XX is either 11 or prime
  • 1≤a_i≤1091 ≤ a\_i ≤ 10^9

예제1

  1. 예제 1

    입력
    2
    3 2 1
    2 6 4
    4 2 3
    7 3 6 9
    
    예상 출력
    2 4 6
    3 6 9 21