Pku1061 problem-solving report frogs' appointment

Source: Internet
Author: User

Description:

 

The two frogs met each other on the Internet. They had a good chat, so they thought it was necessary to meet each other. They are happy to find that they live on the same latitude line, so they agreed to jump westward until they met each other. However, before they set out, they forgot a very important thing. They did not know the characteristics of the other party, nor agreed on the specific location of the meeting. However, frogs are optimistic. They think that as long as they keep jumping in a certain direction, they will always meet each other. However, unless the two frogs jump to the same point at the same time, they will never be able to meet each other. To help the two optimistic frogs, you are asked to write a program to determine whether the two frogs can meet and when.
The two frogs are called Frog A and frog B respectively, and The 0th degree of the latitude line is the origin, from East to West is the positive direction, the unit length is 1 meters, in this way, we get a number axis that is connected at the beginning and end. Set the starting coordinate of frog a to X, and that of frog B to y. Frog A can jump M meters at a time, and frog B can jump n meters at a time. It takes the same time for the two frogs to jump at a time. The total length of the latitude line is l meters. Now you need to find out how many times they will be met.

The input contains only five integers x, y, M, N, and l in a row. x = Y <2000000000,0 <m, n <2000000000,0 <L <2100000000.

 

Outputs the number of jumps required for the meeting. If the meeting is never possible, an "impossible" line is output"

Sample input:

1 2 3 4 5

Sample output:

4

 

Question Analysis:

According to the question, the question requires the minimum integer solution of (x + K * m) mod L = (Y + K * n) mod L; this equation is transformed by the nature of the model: K * (m-n) + T * l = Y-x (t is an integer introduced );

Set a = (m-N); B = Y-X;:

K * A + T * l = B; 0)

See if this formula is familiar. Right, it is the standard equation A * x + B * Y = d that extends the Euclidean method. (here A, B is not a, B) the only difference is that the Extended Euclidean D is the maximum approximate number of the first two coefficients A and B, and B in the 0) formula cannot be guaranteed.

But it doesn't matter. First, extend Euclidean to get a solution K0 and T0 for K * A + T * l = gcd (A, L). Then observe the two formulas:

K * A + T * l = B 0)

K0 * A + t0 * l = D 1) (Order d = gcd (A, L ));

Divide both sides of the 0) formula by D:

K * (A/D) + T * (L/d) = B/d;

Because D is the maximum common divisor of A and B, so A/D, L/D is an integer, so B/d must be an integer, otherwise the equation has no solution;

Multiply the values of the 1) formula by B/d and the value of the 0) formula for comparison:

K * A + T * l = B 0)

(K0 * B/d) * A + t0 * (B/d) * l = B 1)

K = K0 * B/d; t = t0 * B/d;

The next step is to expand from a solution to a solution:

1) divided by B: K0 * (A/D) + t0 * (L/d) = 1;

Because (A/D) and (L/D) are mutually dependent, the equation is written:

(K0 + u * (L/D) * (A/D) + (t0 + V * (A/D) * (L/d) = 1;

As long as u * (L/D) * (A/D) + V * (A/D) * (L/d) = 0 is true;

So k and t can be written as k = (K0 + u * (L/D) * (B/d ); t = (t0 + V * (A/D) * B/d;

The next step is to obtain the minimum integer of K;

Because k = (k0 Mod (L/D) + (U + K0/(L/D) * (B/d );

Therefore, kmin = (k0 * (B/d) mod (L/D );

 

The last step is how to use Extended Euclidean to solve K * A + T * l = gcd (A, L ):

For a * x + B * Y = D;
Recursive call
Make a = B; B = A % B;
Convert it to Form 2) B * x + A % B * Y = D;
Deformation: B * x + A * Y-(A/B) * B * Y = D;
Further Change: A * Y + B (X-A/B * y) = D; 3)

Comparison with formula 2: B * x + A % B * Y = D; 2)
D:
When a = B; B = A % B: x = y; y = x-A/B * Y;

In the call process, X and Y are the solutions of A and B;
When we return to the top layer, a, B are the first a, B, so at this time, X and Y are the solutions;
The function syntax is as follows: (the solution is saved in global variables K and T)
_ Int64 extended_gcd (_ int64 A ,__ int64 B)
{
If (B = 0)
{
K = 1;
T = 0;
D =;
Return;

}
Else
{
_ Int64 tp_gcd;
Tp_gcd = extended_gcd (B, A % B );
_ Int64 temp;
Temp = K;
K = T;
T = temp-(A/B) * t;
Return tp_gcd;
}
}
Attach the AC code:
# Include <stdio. h>
_ Int64 K, T, D;
_ Int64 extended_gcd (_ int64 A ,__ int64 B)
{
If (B = 0)
{
K = 1;
T = 0;
D =;
Return;

}
Else
{
_ Int64 tp_gcd;
Tp_gcd = extended_gcd (B, A % B );
_ Int64 temp;
Temp = K;
K = T;
T = temp-(A/B) * t;
Return tp_gcd;
}
}
 
Int main ()
{
_ Int64 X, Y, m, n, a, B, AA, ll, L;
While (scanf ("% i64d % i64d % i64d % i64d % i64d", & X, & Y, & M, & N, & L )! = EOF)
{
A = m-N; B = Y-X;
If (A <0) {A =-A; B =-B ;}
Extended_gcd (A, L );
If (B % d! = 0) printf ("Impossible/N ");
Else
{
K = K * B/d;
T = T * B/d;
L = L/d;
If (k> = 0)
K = K % L;
Else K = K % L + L;
Printf ("% i64d/N", k );
}
 
}
Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.