POJ 1061 frog dating extensions Euclidean http://poj.org/problem? Id = 1061

Source: Internet
Author: User

Question: Chinese question...
Idea: it is easy to understand from the question, posx + vx * t-posy-vy * t = k * L, that is, the solution to this equation. After simplification, the equation can be written as t * (vx-vy)-k * L = posy-posx, and further reduced to k * L + t * (vy-vx) = posx-posy, L and (vx-vy) can be obtained, that is, known. This equation is a common form of Extended Euclidean equation that we are familiar.
In this case, gcdx = gcd (L, vy-vx). If (posx-posy) % gcdx = 0, the equation has a solution, that is, the frog can meet it, otherwise, it cannot be met.
Next we can use Extended Euclidean. As for the knowledge of Extended Euclidean, there are a lot of Internet resources, so I won't say much here. When we use the Extended Euclidean to obtain a group of solutions for ax + by = 1, we need the smallest positive integer solution. For the question, t must be a positive integer. If the specific solution is negative, we need to deal with it.
Ax + by = pos at this time, another group of solutions, where y is a negative number. Our goal is to change y to the smallest positive number that satisfies the equation. Now let's change the equation. Ax-kab + by + kab = pos, the equation is not changed in essence. We are simplifying it to a (x-kb) + B (y + ka) = pos, to make y positive, you only need to add several a numbers. The specific number can be calculated.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
 
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Typedef long ll;
Ll xx, yy;
Ll gcd (ll a, ll B ){
If (B = 0)
Return;
Return gcd (B, a % B );
}
Void extend_Eulid (ll a, ll B ){
If (B = 0 ){
Xx = 1;
Yy = 0;
Return;
}
Else {
Extend_Eulid (B, a % B );
Int temp = xx;
Xx = yy;
Yy = temp-a/B * yy;
}
}
Int main (){
// Freopen ("1.txt"," r ", stdin );
// Freopen ("3.txt"," w ", stdout );
Ll posx, posy, vx, vy, L;
While (scanf ("% lld", & posx, & posy, & vx, & vy, & L )! = EOF ){
Ll dvx = vy-vx;
Ll dpos = posx-posy;
Ll gcdx = gcd (L, dvx );
If (dpos % gcdx ){
Printf ("Impossible \ n ");
}
Else {
Dvx = dvx/gcdx;
Dpos = dpos/gcdx;
L/= gcdx;
Extend_Eulid (L, dvx );
Yy * = dpos; www.2cto.com
Xx * = dpos;
If (L <0) L =-L;
If (yy> 0 & xx> 0) yy % = L;
Else {
Ll cnt = yy/L;
Cnt =-cnt;
Cnt ++;
Yy = (yy + L * cnt) % L;
}
Printf ("% lld \ n", yy );
}
}
Return 0;
}
Author: wmn_wmn

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.