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)