
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line contains three space-separated integers, ‘A’, ‘B’, and ‘K’, denoting the first weight that he bought, the second weight that he bought, and the weight he wants to lift, respectively.
For each test case, print the number of ways in which Ninja can lift the dumbbell of K kgs by a combination of A and B kg weights.
Print the output of each test case in a separate line.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
Two ways are considered different if the number of times a is used or the number of times b is used is different in the two ways.
1<= T <= 10^4
1 <= A, B <= 10^9
0 <= K <= 10^9
Where ’T’ is the number of test cases, 'A' is the first weight that Ninja bought, ‘B’ is the second weight that Ninja bought, and ‘K’ is the weight Ninja wants to lift respectively.
Time Limit: 1 sec
The idea is to make the mathematical equation of the given problem and iterate over all the possible values. Let’s say we take x weights of A kgs each and y weights of B kgs each, such that the weight that Ninja wants is K kgs.
The equation becomes
This is similar to the equation of a line in the 2D plane. But in the 2D plane, the number of points is infinite. So, we will find the range of values of x and y.
The minimum value of x and y is 0, as we can use only one weight to make the desired weight. The maximum value of x will be when y is 0 and vice versa. So, when x = 0, y = K/B and when y = 0, x = K/A.
So, we will iterate over all the possible values of x and y and count the number of points (i, j) in which the equation holds true.
The steps are as follows:
From the above approach, we deduced that the equation in consideration is
A*x + B*y = K.
Here, we will iterate for y only and then find x from the given equation,
x = (K - B*y)/A.
Here is a condition, this equation only holds true if K - B*y is non-negative and is divisible by A.
So, we will iterate from y = 0 to K/B and check for the above condition.
To optimize the above-discussed solution, we can use the minimum of A and B in place of A.
The steps are as follows:
We found the values in the previous approach by iterating, but if we carefully observe
From the equation,
x = (K - B*y)/A.
if y1 is the smallest value that satisfies the equation, what will be the value that satisfies the equation?
Isn’t it y1 + A?
Further terms satisfying the given equations are y1 + 2*A, y1 + 3*A, …… y1 + p*A.
It is an arithmetic progression with the number of terms = p + 1.
y1 + p*A <= K/B
So, numberOfTerms = (K/B - y1)/A………(a)
In order to decrease the number of computations, we will divide A, B, and K by the greatest common divisor of A and B at the beginning.
The steps are as follows:
Continuing from the previous approach, the idea is to use the Extended Euclid Algorithm to find the first value of x divisible by B.
From the equation,
x = (K - B*y)/A
We have to find smallest value of y(let’s say y1) such that,
(K - B*y1)%A = 0
K%A - (B%A)(y1%A) = 0
As y1 will always be lesser than A,
we get
y1 = (K - A*x)/B….from the above equation
Taking modulo both the sides,
y1%A = (K/B)%A - (A*x/B)%A = (K/B)%A…….Equation (b)
The above term (A*x/B)%A will be equal to 0 as the inside term is a multiple of A.
So, the equation becomes,
y1%A = (K/B)%A
So, we will calculate (K/B)%A. Now, we have the value of y1%A, but we need the value of y1, not y1%A. So, we have to find the modular inverse of y1%A with respect to A. Here, Extended Euclid Algorithm will be used.
Here is an excellent link to learn how to find the modulo inverse using the Euclid algorithm.
The overview is:
We will calculate the gcd of A and B-> Divide A, B, and K by the gcd of A and B -> then get the value of y1 from equation (b) -> Find the number of terms(p) -> number of Terms + 1 is the final answer.
The steps are as follows: