DAY 3 (bonus problem) - Game Of Discs

3.6

5 votes
Extended-Euclide, Easy
Problem

Nikhil has invented many games for kids. So, he has become very popular among the next generation kids.

Komal and Raj were playing one of those games.

The game rules are as follows.

  • Initially they both had their separate pile of some discs.

  • Komal had X discs while Raj had Y discs on his pile. They also had a common source of infinte identical discs.

  • The objective of the game was to make the heights of two piles as balanced as possible by adding some discs from the source to their own pile.
  • The only condition was that Komal can pick discs only in multiples of A and add them to her pile while Raj can pick discs in
    multiples of B and add them to his pile.

You see, because of this the piles can't always become equal. You have been given a very simple task, find the minimum absolute difference achievable between the heights of the two piles. You also need to specify the sequence of moves to achieve that state.

INPUT

The first line of input contains a single integer T denoting number of test cases. Each of the next T lines would contain four positive integers A, B, X, Y as specified in the question.

OUTPUT

For each test case output two lines.

The First line should contain two space separated integers, the first integer N denoting the minimum absolute difference possible between the piles, the second integer M denoting the total number of times either Raj or Komal pick the stones from the pile.

In the following line, print a sequence of M non negative integers. The integers at odd positions denoting number of discs picked by Komal and those at even positions denoting number of discs picked by Raj.

If you think there are multiple solutions to the same problem, you can print any one of the solutions.

The kids can only count upto 1015 discs at a time.

Also as the two kids have to complete their homework afterwards, they want to finish the game sooner. So, in your output the total number of times either Raj or Komal pick the discs from the pile should not be more than 20.

CONSTRAINTS

1≤T,A,B,X,Y≤1000000

Time Limit: 0.5
Memory Limit: 256
Source Limit:
Explanation

In first test case the minimum achievable difference of heights of heights of file will be 4. One of the ways they achieve it is as follows: Height: 4 10 Komal adds 10 discs to her pile. Height: 14 10 There is no way to achieve a difference of less than 4.

In second test case the two heights are already equal so the difference is zero. Height: 15 15 Adding another 15 discs to both will also keep the height same. Height: 30 30

In third test case Height: 10 11 Komal adds 6 discs to her pile Height: 16 11 Raj adds 4 discs to his pile Height: 16 15 Komal again adds 3 discs to her pile Height: 19 15 Raj adds 4 discs to his pile Height: 19 19

Editor Image

?