扩展欧几里得
扩展欧几里得求不定方程
题目链接:http://poj.org/problem?id=1061
$x\times a+y\times b=gcd(a,b) \rightarrow y\times b+\left ( x-\frac{a}{b}\times y \right )\times \left ( a\%b \right )=gcd(a,b)$
对于知道特解$x_{1}$和$y_{1}$通解为:$x=x_{1}-t\times \frac{b}{gcd(a,b)} $ $y=y_{1}-t\times \frac{a}{gcd(a,b)}$ $t\epsilon 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;
}

浙公网安备 33010602011771号