Poj 1061 frog appointment (Extended Euclidean), poj1061
Link: poj 1061
Solution: Extend Euclidean application: obtain a group of solutions of the equation Ax + By = C (x0, y0 ).
The Q & A equation is available for the Q &:
X + mt = y + nt + CL
--> X-y = (n-m) t + CL and (x-y), (n-m), L known. Is to find the minimum positive integer solution t that satisfies the equation.
Theorem: Set a, B, and c to any integer. If a group of integers in equation ax + by = c is resolved as (x0, y0 ),
Then it can be written as any integer solution (x0 + kb ', y0-ka'), where a' = a/gcd (a, B ), B '= B/gcd (a, B), k is any integer.
Deduction: Set x0 to the nearest zero of All Integer Solutions, and set the exgcd () to x1. Then x0 = x1 % B '= x1 % (B/gcd (a, B ))
If x0 is not negative, x0 is the smallest positive integer solution. If x0 is a negative number, x0 + B 'is the smallest positive integer solution.
Note: longlong is required for large data volumes.
#include<stdio.h>__int64 gcd(__int64 a,__int64 b){ return b==0?a:gcd(b,a%b);}void exgcd(__int64 a,__int64 b,__int64 &x1,__int64 &y1){ __int64 t; if(b==0){ x1=1; y1=0; return ; } exgcd(b,a%b,x1,y1); t=x1; x1=y1; y1=t-a/b*y1;}int main(){ __int64 m,n,x,y,l,c,x1,y1; scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l); c=gcd(l,(m-n)); if((y-x)%c!=0) printf("Impossible\n"); else{ exgcd(l,m-n,x1,y1); y1=(y-x)/c*y1; y1=y1%(l/c); if(y1<0) y1+=l; printf("%I64d\n",y1); } return 0;}
Questions about ACM related to Extended Euclidean Algorithms
To obtain the smallest positive integer. X0 is a special solution, so (X0% B) is also a specific solution.
ACM issues, frog's appointment
This is the same problem. Use the Extended Euclidean algorithm ~