http://172.20.6.3/Problem_Show.asp?id=1371
http://www.cnblogs.com/jackge/archive/2013/04/22/3034925.html详细的题解,大概是网上能看到的最简单易懂的扩展欧几里得讲解了
代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 const int maxn=100010; 8 void exgcd(int a,int b,long long &x,long long &y){ 9 if(!b){ 10 x=1;y=0; 11 return; 12 } 13 exgcd(b,a%b,x,y); 14 long long w=x;x=y; 15 y=w-y*(a/b); 16 } 17 long long gcd(long long x,long long y){ 18 while(y){ 19 int w=y;y=x%y;x=w; 20 } 21 return x; 22 } 23 int main(){ 24 long long x,y,m,n,l; 25 scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l); 26 long long t,z=x-y,zz=n-m,k,d=gcd(n-m,l); 27 if(z%d) printf("Impossible\n"); 28 else{ 29 z/=d; 30 exgcd(zz/d,l/d,t,k); 31 t=z*t-z*t/l*l; 32 if(t<0)t+=l; 33 printf("%I64d\n",t); 34 } 35 return 0; 36 }
时间: 2024-10-05 04:19:33