LEX_GCD
시간 제한1.5초메모리 제한1024 MB
임의의 K개 원소 gcd를 모두 보존하는 순열 중 사전순으로 가장 작은 것을 찾되, 원소 하나에 소수 X를 곱하거나 곱하지 않을 수 있다.
문제
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 considered -gcd equivalent if for every set of distinct indices from to , 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 by a given integer before finding this lowest -gcd equivalent sequence. You are allowed to not multiply by at all and keep the same sequence. Additionally, it is guaranteed that is divisible by only and itself. You aim to minimize the resulting sequence lexicographically among all possible choices of preprocessing (or choosing not to) of . Note that if you decide to preprocess the sequence, then your result must be a -gcd equivalent of the preprocessed sequence (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 , the number of test cases. Each test case consists of three positive integers , , and , followed by positive integers .
출력
For each test case, output integers representing the lexicographically lowest -gcd equivalent sequence to after performing the allowed preprocessing.
제한
- (over all test cases)
- , is either or prime