Frog Dating-POJ 1061 (extended Euclid)

Source: Internet
Author: User

analysis: This thing in number theory should be called indefinite equation, you can search, there is a very good proof, first find out a set of special solutions to the equation, and then use this group of special solutions to find the general solution, but how to find out after the special solution of the minimum non-negative x value? We know that x = x0 + bt, assuming x=0, which is the minimum value, then T = x0/(-B), x0+x0/(-B) *b is the minimum value, of course, if the result is negative plus a B is available.

The code is as follows:

=============================================================================================================== =================

#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespacestd;Const intMAXN =107;Const intOO = 1e9+7; typedefLong LongLL; ll EXGCD (ll A, ll B, LL&x0, LL &y0) {    if(b = =0) {x0=1, y0 =0; returnA; } LL D= EXGCD (b, a%B, x0, y0);    Swap (x0, y0); Y0= y0-a/b * x0;///the original is y0 = x0-a/b * Y0;    returnD;}intMain () {LL x1, x2, L1, L2, L; ///Frog AB's starting position x1,x2///Frog ab Each jump length l1,l2///Latitude bus length l///assuming the total jumps x times, then frog A's jumping length is x1 + x*l1///Frog B's jumping length is x2 + x*l2///(X1+X*L1)-(X2+X*L2) = Y*l ==> (l2-l1) *x+ (L) *y = x1-x2;Cin>> x1 >> x2 >> L1 >> L2 >>L; LL a= L2-l1, B = L, c = x1-x2, x0, y0; LL D=EXGCD (A, B, x0, y0); ifCd) {///conditions do not meetprintf"impossible\n"); return 0; } x0= x0 * (C/D), y0 = y0 * (c/d); A/= D, b/=D; LL x= x0+x0/(-b) *b; if(X <0) x + =b; cout<< x <<Endl; return 0;}

Frog Dating-POJ 1061 (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.