Frog Dating---poj1061 (extended Euclid)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1061

is to find contentment (X+MT)-(Y+NT) = Lk's T and K

The upper formula can be reduced to (N-M) T + Lk = x-y, satisfying the form of ax+by=c so that we can seek t and K with the extension Euclid;

Since the upper formula has a solution and only if C is gcd (A, B) = 0;

#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<algorithm>#include<map>#include<queue>#include<stack>#include<math.h>using namespacestd;#defineMet (A, b) memset (A, B, sizeof (a))#defineN 10053#defineINF 0x3f3f3f3fConst intMOD = 1e9+7; typedefLong LongLL; ll GCD (ll A, ll b) {returnb = =0? A:GCD (b, a%b);}voidEX_GCD (ll A, ll B, ll &x, LL &y) {    if(b = =0) {x=1; Y=0; return ; } EX_GCD (b, a%b, x, y); LL T=x; X=y; Y= t-a/b*y;}intMain () {LL X, Y, L, N, M;  while(SCANF ("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l)! =EOF)        {LL A, B, X, Y, C; A= n-m, B = L, c = xx; LL R=gcd (A, b); if(c%R) {puts ("Impossible"); Continue; } A= A/R; b= bR; C= c/R; ///The reason they are divided by R is to let AB coprime, then the result is the equivalent of x and y c times;EX_GCD (A, B, X, y);///at this time A and B coprime, the solution is Ax+by=1, the final solution to *c;x= x*C; X= x% B;///The minimum solution is required, so the remainder of the B is to be obtained;         while(x <=0) {x+=b; } printf ("%lld\n", x); }    return 0;}
View Code

Frog Dating---poj1061 (extended Euclid)

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.