


Harry chooses zero or more wands of power ‘A’ and zero or more wands of power ‘B’. He chose exactly ‘K’ wands.
Then Harry adds the power of all ‘K’ wands and made a new wand of power equal to the sum of powers of ‘K’ chosen wands.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains three space-separated integers ‘A’, ‘B’, and ‘K’.
For each test case, print the powers of all unique wands in sorted order separated by a single space.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= K <= 5000
0 <= A, B <= 10 ^ 5
Time Limit: 1sec
The idea here is to generate all possible combinations of wands. At each step, we have two choices either combine a wand of power ‘A’ or combine a wand power ‘B’. So, we will solve this problem recursively by making two recursive calls at each step, one for power ‘A’ and other for power ‘B’.
Algorithm:
Description of ‘doRecursive’ function.
This function is used to generate all possible combinations of wands.
This function will take six parameters.
doRecursive(A, B, current, K, answer, hashSet):
The idea here is to use that the previous approach as overlapping subproblems property. So we can use dynamic programming to avoid repetitive computation.
Example :
The subproblems marked in orange color are overlapping subproblems so we will use dynamic programming to store them.
Algorithm:
Description of ‘doRecursive’ function.
This function is used to generate all possible combinations of wands.
This function will take seven parameters.
doRecursive(A, B, current, K, answer, hashSet):
The idea here is to generate all possible combinations of wands. We will run a loop from i = 0 to K. Then we can make a combination by taking wand of power equals (‘A’ * i) times and wand of power equals (‘B’ * ‘K – i’ ) times. We will use a hash set to keep track of duplicate combinations.
Algorithm: