扩展欧几里得求不定方程
题目链接:http://poj.org/problem?id=1061
$x imes a+y imes b=gcd(a,b) ightarrow y imes b+left ( x-frac{a}{b} imes y ight ) imes left ( a\%b ight )=gcd(a,b)$
对于知道特解$x_{1}$和$y_{1}$通解为:$x=x_{1}-t imes frac{b}{gcd(a,b)} $ $y=y_{1}-t imes frac{a}{gcd(a,b)}$ $tepsilon Z$
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define ll long long ll gcdd(ll a,ll b,ll &x,ll &y)// a*x+b*y=gcd(a,b) { if(b==0) { x=1; y=0; return a; } ll res=gcdd(b,a%b,x,y); ll temp=x; x=y; y=temp-a/b*y; return res; } int main() { ll x,y,m,n,L,k,T; while(cin>>x>>y>>m>>n>>L) { ll gcd=gcdd(m-n,L,k,T); if((y-x)%gcd!=0) { cout<<"Impossible"<<endl; } else { ll mmp=(y-x)/gcd; k*=mmp; ll k0=k%abs(L/gcd); if(k0<0)k0+=abs(L/gcd); cout<<k0<<endl; } } return 0; }