Frog's Dating POJ1061

The male frog is at the x position at first, and the female frog is at the y position. The male frog jumps m meters each time, and the female frog jumps n meters each time, all jumping to the right. The length of the earth's meridian is L, and then the earth is round, that is to say, jumping to L, L+1, L+2...In fact, jumping to 0, 1, 2. The male frog wanted to chase the female frog and asked how many times they could jump together. If they can never meet, output Impossible (so poor!) 

Obviously, just ask for a k such that x + k*m ≡ y + k*n (mod L), right? At least I want to drop that! Then simplify the equation, it becomes (nm) * k ≡ xy (mod L). Then this equation is actually equivalent to (nm)*k + L*s = xy. This is the model of ax + by = c for integer x.

It is not that simple to ask for the integer x solution of ax + by = c, otherwise I won't have to spend a whole night! First, let d = gcd(a, b), divide both sides of the equation by d to get a/d * x + b/d * y = c/d. Obviously, a is divisible by d, and b is also divisible by d. And x and y are integer solutions, so c/d is also required to be integers. If c does not divide d, of course it is Impossible. Otherwise, if we can find the solutions x0 and y0 of ax0+by0=d, then multiply both sides by c/d, that is, a(c/d * x0) + b(c/d * y0) = c, you can get The solution of the original equation x = (c/d * x0), y = (c/d * y0).

Hey hey hey, wait a minute, how do you know that ax0 + by0 = d must have a solution? This has to be rigorously proved:

Theorem 1: If d = gcd(a, b), then positive or negative integers k and l must be found, so that d = a*k + b*l.

Proof : Since gcd(a, 0) = a, we can assume b ≠ 0, so that we can write

a = b*q1 + r1

b = r1*q2 + r2

r1 = r2*q3 + r3

……

 From the first formula, r1 = a-q1*b, so r1 can be written in the form of k1*a + l1*b (in this case, k1 = 1, l1 = -q1). From the second formula, r2 = b-r1*q2 = b-(k1*a + l1*b)*q2 = -q2*k1*a + (1-q2*l1)*b = k2*a + l2* b.

Obviously, this process can be repeated through this series of remainders until we get an expression rn = k*a + l*b, that is, d = k*a + l*b, which is what we want to prove.

 

So, ax0 + by0 = d must have a solution! So how should x0 and y0 be found? To use an algorithm called Extended Euclid (NND, I heard it for the first time, I am still too weak~~~)!

The solution is as follows: Since gcd(a, b) = gcd(b, a%b) (you don’t need to prove this, right? Everyone knows this!), there is a x0  + b y0  = gcd(a, b) = gcd (b, a%b) = bx1 + (a%b)y1, and a%b can be written as aa/b*b (a/b*b is not equal to a! Remember to be scolded for this SB problem~ ), so = bx1 + (aa/b*b)y1 = a y1  + b (x1-a/b*y1) , so if we find gcd(b, a%b) = bx1 + (a%b) The x1 and y1 of y1, then you can find x0 = y1, y0 = (x1-a/b*y1) through observation. How do we find x1 and y1? Of course it is to ask for x2 and y2, the same is true. Keep asking until gcd(an, 0) = an*xn + 0 * yn, then set xn=1, yn=0 and you are done, you can find xn-1 and yn-1, then xn-2 and yn-2 , And then have been seeking to x0 and y0.

So get ax0 + by0 = gcd(a, b) to find the extended Euclidean algorithm for integers x0 and y0 (I'm so annoying here! I almost gave up on this step! Weak, weak, 55555~~ ~)

long long extgcd(long long a, long long b, long long &x, long long &y)
{
    long long d, t;
    if (b == 0) { x = 1; y = 0; return a; }
    d = extgcd(b, a % b, x, y);
    t = x - a/b*y; x = y; y = t;
    return d;
}

 

long long? Why long long? Because I used int to give Wrong Answer! Then Baidu search found that there is a long long thing, that is, int is 32 bits, and long long is 64 bits, which can represent larger integers. This question is disgusting, why are you bullying me? ? ! ! But then you can find x0 and y0.

Then find ax + by = c, and judge that c is Impossible if it is not divisible by d, otherwise, let x = c/d * x0 to get an x ​​solution.

But there are countless solutions satisfying ax + by = c. What about the god horse? ax + by = c is actually equivalent to ax ≡ c (mod b), right? If I get a special solution x*, then adding several times b is still the solution to this equation, because a(x*+k*b) = ax* + a*k*b ≡ c + 0 ≡ c (mod b) . Therefore, the equation must have an integer solution on [0, b-1] (if it is less than 0, you add several times b, you can keep it in 0~b-1; if it is greater than b-1, you subtract Several times b, it also keeps 0~b-1).

So how to find the smallest non-negative integer x? Two more theorems are used:

Theorem 2: If gcd(a, b) = 1, then the equation ax ≡ c (mod b) has a unique solution on [0, b-1].

Proof: From theorem 1 , we can always find positive or negative integers k and l such that a*k + b*l = gcd(a, b) = 1, that is, we can find ax ≡ 1 (mod b) Solution x0. Of course, multiplying both sides by c has a(cx0) ≡ c (mod b), so x = cx0 is the solution of ax ≡ c (mod b). Since adding or subtracting several times b is the solution of this equation, x has a solution on [0, b-1]. So how to determine its uniqueness? It took me an hour to finally prove it. The proof method is, assuming that x1 and x2 are both solutions on [0, b-1], then there are ax1 ≡ c (mod b), ax2 ≡ c (mod b), Subtracting the two formulas is a(x1-x2) ≡ 0 (mod b), that is, a(x1-x2) can be divisible by b. But gcd(a, b) = 1! So there is no common language between a and b to communicate, so it can only be said that (x1-x2) is divisible by b. But x1 and x2 are both on [0, b-1], so x1-x2 is also on [0, b-1], so we can only say that x1-x2=0, so x1=x2. This proves the uniqueness of understanding!

This theorem is just for the convenience of proving Theorem 3. Theorem 3 is the kingly way:

Theorem 3: If gcd(a, b) = d, then the equation ax ≡ c (mod b) has a unique solution on [0, b/d-1].

Proof: As mentioned above, this damn equation is equivalent to ax + by = c. If there is a solution and both sides are divided by d, there is a/d * x + b/d * y = c/d, which is a/ d * x ≡ c/d (mod b/d), obviously gcd(a/d, b/d) = 1, so we know from Theorem 2 that x has a unique solution on [0, b/d-1]. Therefore, the x of ax + by = c has a unique solution on [0, b/d-1], that is, ax ≡ c (mod b) has a unique solution on [0, b/d-1]. Prove!

With the above damn theorems, Xiao Cai finally solved the problem of the smallest non-negative integer. If we get a particular solution X of ax ≡ c (mod b), then I set r = b/gcd(a, b), we know that x has a unique solution on [0, r-1], so I use x = (X% r + r)% r can find the smallest non-negative integer solution x! (X% r may be a negative value, at this time it stays in [-(r-1), 0], and a positive value stays in [0, r-1]. Add r to keep it in [1, 2r- 1], so modulo r will be in [0, r-1]).

With so many proofs and so many words, only 21 lines of code were exchanged, helpless! Life is too short! ACM, I want to say that loving you is not easy! Post the code decisively below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include <iostream>

using namespace std;

long long extgcd(long long a, long long b, long long &x, long long &y)

{

    long long d, t;

    if (b == 0) { x = 1; y = 0; return a; }

    d = extgcd(b, a % b, x, y);

    t = x - a / b * y; x = y; y = t;

    return d;

}

int main()

{

    long long x, y, m, n, L, X, Y, d, r;

    while (cin >> x >> y >> m >> n >> L)

    {

        d = extgcd(n - m, L, X, Y); r = L / d;

        if ((x - y) % d) cout << "Impossible" << endl;

        else cout << ((x - y) / d * X % r + r) % r << endl;

    }

    return 0;

}

Guess you like

Origin blog.csdn.net/qq_40596572/article/details/104881733