 What's up guys? Mike the coder. Today we are going to talk about linear diaffetine equation. So the linear diaffetine equation is an equation of the form ax plus by equal to c and ab and c are integers and x and y are unknowns. For classical problems involving this equation, there are four things that you might want to find. One is finding one solution and the other one is finding all solutions. The third one's finding the number of solutions and the solutions themselves given an interval and the fourth one is finding a solution with the minimum value of x plus y. So how do you find a solution to this equation? Well, you could actually find one solution given the two unknowns using the extended Euclidean algorithm. First, if you use extended Euclidean algorithm, what it would give you is you it would give you the greatest common divisor, g, and then it gives you the two numbers that give you the linear combination when you multiply them and add them. So you would get the equation a xg plus b yg equals a g. So xg and yg are the two numbers from the linear combination that was that gave that was given to us from the extended Euclidean algorithm. So now let's compare the equations of the original equation a x plus b y equal to c where x and y are unknowns and our new equation a xg plus b yg equal to g. So remember g is the greatest common divisor of a and b, right? So if you compare these two equations, you'll realize that there's only a solution if c is divisible by g, right? And it if you could just like if you divide the two equations, you could actually see it and the proof here below does divide the equation. So if you look at the new equation after dividing by g, you'll see that we get this equation a times xg times c over g plus b times yg times c over g equal to c and you'll realize that the answer of x is going to equal to xg times c over g and the answer for y is going to be yg times c over g. Here's the code in c plus plus. The first part is getting used basically the extended Euclidean algorithm GCD again. That's the first method and that just gives us the linear combination of x and y and it gives us the GCD of a and b. The next function is finding any of the solution, which is exactly what we're going to do. Basically first calls the extended Euclidean algorithm GCD passing in the numbers of x0 and y0 and the values of absolute value a and b. This is going to spit us back the greatest common divisor g. Okay. If c is not divisible by g, it's just going to return false, right, because that means it's not possible. Otherwise, we get the the two numbers that was spit back from the GCD call of greatest common divisor and we're going to multiply them by c over g both x0 by c over g y0 by c over g. Then if any of the numbers were negative, we just set it to the negative value. In the end, we just return true. So at the end of this this code, you're basically just going to get new numbers x0, y0 and those new numbers x0, y0 are going to be the solutions to this equation. Now, what about finding all solutions to the equation? Well, if we go back to our initial equation and remember now we have our solutions x0 and y0 to the first equation for the first two equations, right? And you realize that if we add b over g to the x0 and we subtract a over g from y0, our answers are still the same equal to c. So what we could do is repeatedly add b over g to our x0, which I answer for our x value, and repeatedly subtract a over g from our answer for our y value. If k is the number of times that we are able to add b over g to our x value and subtract a over g from our y value, then the answer for all the solutions of the equations is going to be x is equal to x0 plus k times b over g and y is equal to y0 minus k times a over g. So now let's assume that we want to find the values now constrained to an interval. So we're given the min x max x in an interval of our x values and the min y max y of an interval of our y values. So now we need to find solutions between these intervals. So how do you solve this problem? Well, what you could do is you could find the minimum value of x that's just greater than or equal to the left side of the min x, which is a left interval. We could do this with finding any solution with the diaphthine equation we discussed earlier. Then we could shift the solution to get a value that's greater than or equal to min x based on what we know from all the number of solutions from that equation. So we call this new value that we're shifting by l. And then we do the same thing of x using the max x. So now we're going to find the maximum value of x that's just smaller than less than or equal to our right boundary of max x and we could denote this by like rx. Then we do the same thing for y. So find the minimum value of y that's just greater than or equal to min y and then find the maximum value of y that's just less than max y. And then now we have then what we have is now we have our two intersection intervals of lx1, rx1, and lx2, rx2. So now we have these intersections. Now it's the answer is just the solutions to all the values of the intersections between both of these intervals. And yeah, so yeah, basically, the code is pretty long, but it looks something like this. Solution is just going to shift the x and y by a certain number of count by a certain number value of count. So it adds this count of B to x and then subtract y from A from y, count times A from y. So now we're in the function find all the solutions. Here, we're given A, B, and C. And now we have the min x, max x, min y, max y. Okay, so now what's going to do the first part is if it can't find any solutions, right, which is using the diaphytine problem, I didn't define the function yet. That's why it's squarely line. But if you can't find anything, it returns zero. Otherwise, we're going to take A and we're going to divide it by G, which is the greatest common divisor, and B divided by the greatest common divisor. Okay, this is just getting the signs of sine A and sine B. Okay, so now what we're going to do is we're going to shift the equation by the min x minus x divided by B. So this is like the interval that you're shifting it by. So the interval you're shifting it by the minimum value of x, what we have, which is a left bound of our expert value, right, left bound, and we're going to subtract it by x, right, and then we're going to divide it by B. So that's going to be like the number of times we could shift it by B, because that's all the number of times we could shift it. So this is going to just shift the solutions. For x values less than min x, we're going to now shift the solution again, because now we got like too far of min x, and then we're going to change the sign, right, we're going to change the sign with a different sign. Okay, and it goes that that the other way. And then if x is greater than max x, that means we're too far, right, we're past the interval, right, so then we're going to return zero. One x one, and that's just a copy of x. Okay, and then this one is shifts the solution by the interval of max x minus x over B. It's going to shift that number times. I think it's for the wide wide wide wide now. Yeah, yeah. So this is shift number times makes B negative if x is greater than max x is just checking for the bounds again. Now we have the right side, right side of our x. And then it does the same shift again for the y values. So here, here, if y is greater than the right part of our interval, return zero. So now once you have both of the shifts, we have Rx, Rx two, Lx two, Rx two. So these are the left bounds of our final solutions, right? The left bounds of our final solution. So then we get our new new boundaries of our final solution. So that's what this is for. And that's what this is for. And then at the at the end, we return the right boundary minus the left divided by B, which is the number of times for each of those. So I'll count the number of solutions and we add by one. So that gets your new boundary. Now, what if I want to find the solution with the minimum value of x plus y? Well, it's basically the same idea. You just shift the solution to satisfy some conditions. So then in the end, you're going to get a set of all solutions. And now we just want to find the minimum x and then the minimum y. So you would, let's say the minimum x is x prime, and then the minimum wise y prime, and then you would get the same equations as before. And then if you add them together, you would realize that a faster way you would actually get is x plus y plus k times b minus a over g. So yeah, that's actually a faster way to do it. But keep in mind, you don't know what k is, because k is the number of times you're doing this, right? But yeah, that would be a faster way to do it. But yeah, read, comment, subscribe. I hope you guys enjoyed this video. I'll check you guys later. Peace.