题目:http://poj.org/problem?id=1061
思路:扩展欧几里得
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <cstdio> using namespace std; long long gcd(long long a,long long b) { if(b==0) return a; return gcd(b,a%b); } long long exgcd(long long a,long long b,long long &x,long long &y) { if(b==0) { x=1; y=0; return a; } else { long long ans=exgcd(b,a%b,x,y); long long t=x; x=y; y=t-a/b*y; return ans; } } int main() { long long x,y,m,n,l; /// (x+mt) - (y+nt) = kl // (x-y) + (m-n)t = kl // (m-n)t - kl = y-x cin>>x>>y>>m>>n>>l; long long xx,yy; long long A=-(n-m),B=-l,C=-x+y; long long g=gcd(A,B); if(C%g!=0) cout<<"Impossible "; else { A/=g; B/=g; C/=g; exgcd(A,B,xx,yy); xx*=C; xx=(xx%B+B)%B; cout<<xx<<endl; } return 0; }