Monk and Fredo
Fredo and you are talking about a movie that you have recently watched while Monk is busy teaching Number Theory. Sadly, Monk caught Fredo talking to you and asked him to answer his question in order to save himself from punishment. The question says:
Given two weights of and units, in how many different ways you can achieve a weight of units using only the given weights? Any of the given weights can be used any number of times (including number of times).
Since Fredo is not able to answer the question, he asked you to help him otherwise he will get punished.
Note: Two ways are considered different if either the number of times is used or number of times is used is different in the two ways.
Input Format:
The first line of input consists of an integer denoting the number of test cases.
Each test case consists of only one line containing three space separated integers , and .
Output Format:
For each test case, print the answer in a separate line.
Constraints:
Given two weights of and units, in how many different ways you can achieve a weight of units using only the given weights? Any of the given weights can be used any number of times (including number of times).
Since Fredo is not able to answer the question, he asked you to help him otherwise he will get punished.
Note: Two ways are considered different if either the number of times is used or number of times is used is different in the two ways.
Input Format:
The first line of input consists of an integer denoting the number of test cases.
Each test case consists of only one line containing three space separated integers , and .
Output Format:
For each test case, print the answer in a separate line.
Constraints:
Explanation
Test case :
can only be achived by using two times and one time.
Test case :
can't be achieved by using and . So, ways are there.
Test case :
can be achieved by using times and times . So, there is only one way,
Test case :
can be achieved by using three times or two times. So, there are two different ways of getting .
can only be achived by using two times and one time.
Test case :
can't be achieved by using and . So, ways are there.
Test case :
can be achieved by using times and times . So, there is only one way,
Test case :
can be achieved by using three times or two times. So, there are two different ways of getting .
Brief Description: Given , and , find the number of pairs such that and .
Prerequisite: Euclid Extended Algorithm
Difficulty: Medium
Detailed Explanation:
As we know that is a linear Diophantine equation in two variables. The solution of this equation exists if and only if is divisible by the .
Solution 1:
The simplest solution is to try all the values of and . As we know that the values of will be in range from . Similarly the values of will be in the range . Time complexity of this solution is which won't run within the time limit.
Solution 2:
We know that for one value of there is at-most one which will satisfy this equation. So, iterate over all values of in the range and check if is divisible by or not. Time complexity of this solution is which is better than previous solution's complexity but still not good enough to pass within the time limit.
Note: We can also iterate over all values of from and check if is divisible by or not. But , so .
Solution 3:
Let be the smallest non negative number such that is divisible by . We can say that is the first value of such that is divisible by .
If we have , the next value of will be .
Why ??
Let .
But we know that is divisible by and is also divisible by , so will also be divisible by .
So, the values of will be where .
The values of seems to be a Arithmetic Progression. So we can easily calculate number of terms in the Arithmetic progression using equation.
So number of terms in the Arithmetic progression is equal to .
Now the only part thats left is to find the value of . An obvious solution is to iterate over all values of and break as soon as we find the first value such that is divisible by . Time complexity of this solution is in worst case. But this solution is faster then previous solutions in other cases.
Exercise: What is the worst case for this solution ?
Solution 4:
Note: A small optimization is divide both the side of the equation by the . Why ?? Think about it.
We can compute the first value of using Modulo Inverse (Extended Euclid).
Taking modulo both side.
Now we need to check if is divisible by .
Let
So is the first value of such that is divisible by .
We can compute in and number of term using the formula computed in the previous solution ( ) in .
So the overall time complexity is .
Time Complexity:
Space Complexity:
Prerequisite: Euclid Extended Algorithm
Difficulty: Medium
Detailed Explanation:
As we know that is a linear Diophantine equation in two variables. The solution of this equation exists if and only if is divisible by the .
Solution 1:
The simplest solution is to try all the values of and . As we know that the values of will be in range from . Similarly the values of will be in the range . Time complexity of this solution is which won't run within the time limit.
Solution 2:
We know that for one value of there is at-most one which will satisfy this equation. So, iterate over all values of in the range and check if is divisible by or not. Time complexity of this solution is which is better than previous solution's complexity but still not good enough to pass within the time limit.
Note: We can also iterate over all values of from and check if is divisible by or not. But , so .
Solution 3:
Let be the smallest non negative number such that is divisible by . We can say that is the first value of such that is divisible by .
If we have , the next value of will be .
Why ??
Let .
But we know that is divisible by and is also divisible by , so will also be divisible by .
So, the values of will be where .
The values of seems to be a Arithmetic Progression. So we can easily calculate number of terms in the Arithmetic progression using equation.
So number of terms in the Arithmetic progression is equal to .
Now the only part thats left is to find the value of . An obvious solution is to iterate over all values of and break as soon as we find the first value such that is divisible by . Time complexity of this solution is in worst case. But this solution is faster then previous solutions in other cases.
Exercise: What is the worst case for this solution ?
Solution 4:
Note: A small optimization is divide both the side of the equation by the . Why ?? Think about it.
We can compute the first value of using Modulo Inverse (Extended Euclid).
Taking modulo both side.
Now we need to check if is divisible by .
Let
So is the first value of such that is divisible by .
We can compute in and number of term using the formula computed in the previous solution ( ) in .
So the overall time complexity is .
Time Complexity:
Space Complexity:
#include<bits/stdc++.h>#define dd double#define ll long longusing namespace std;ll g, x, y;void extendedEuclid(ll A,ll B) {if(B == 0) {g = A;x = 1;y = 0;}else {extendedEuclid(B, A%B);ll temp = x;x = y;y = temp - (A/B)*y;}}int main(){int q;cin>>q;while(q--){ll a,b,d,e=0;cin>>a>>b>>d;extendedEuclid(a,b);if(d%g !=0){cout<<"0\n";continue;}ll k1=ceil(dd(-x)*(dd(d)/dd(b))),k2=floor((dd)y*((dd)d/(dd)a));if(k1<=k2)cout<<k2-k1+1<<"\n";elsecout<<"0\n";}return 0;}
everything is pretty clear , just one thing is bugging me .
ReplyDeletewhy you put y1 = y%a in the first place before checking that it will make the solution for the equation .
apart from hit and trial what was the logic behind .