Elimination Round, Problem F
Time limit2sMemory limit512 MB
Given positive a_i and d, decide whether nonzero x_i exist with sum a_i x_i = d, and if so build the unique sequence defined by the greedy rule minimizing each |D_i|.
- Level
Hard8 of 10
- Topics
- Number theory, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Gnome Paul is competing in the elimination round of the Gnome Math Cup. Problem F reads as follows.
You are given positive integers and a positive integer . Find nonzero integers with such that
Gnomes dislike big numbers, so every and is at most , and no may be .
Many sequences can satisfy the equation, so exactly one of them counts as the answer. Set and, for , set , so is the greatest common divisor of . A required sequence exists if and only if divides . When it exists, build it from left to right. Put , and for pick this way.
- is not , and is not and is a multiple of .
- Among all such , take one that makes as small as possible.
- If two of them give the same , take the smaller .
The last value is . For nothing is picked and . Every value this rule produces satisfies .
Input
The first line contains one integer , the number of tests (). The tests follow.
The first line of each test contains two integers and (, ). The second line contains integers (). The sum of over all tests does not exceed .
Output
Print the answer for each test. If the required sequence exists, print YES on one line, then print separated by single spaces on the next line. Only the sequence defined above is accepted. If there is no such sequence, print NO as the only line for that test.