| Run ID | User | Problem | Result | Memory | Time | Language | Code Length | Submit Time | 
| 7643827 | kingpro | 1061 | Accepted | 172K | 0MS | C++ | 497B | 2010-09-20 16:19:25 | 
#include <stdio.h>
typedef long long i64;
i64 gcd(i64 a,i64 b){ return b?gcd(b,a%b):a; }
i64 ext_gcd(i64 a,i64 b,i64& x,i64& y){ i64 t,ret; if (!b){ x=1,y=0; return a; } ret=ext_gcd(b,a%b,x,y); t=x,x=y,y=t-a/b*y; return ret;}
int main()
{
	i64 x, y, m, n, L, s;
	scanf("%lld %lld %lld %lld %lld", &x,&y,&m,&n,&L);
	n-=m, x-=y, s=gcd(L, n),
	(x%s!=0 ? printf("Impossible\n") : (L/=s, n/=s, x/=s, ext_gcd(L, n, m, y), s=x*y/L, y=x*y-s*L, y<0 && (y+=L), printf("%lld\n", y)));
	return 0;
}
有x+mt-(y+nt)=kL => kL+(n-m)t=x-y => kL+nt=x, (n-=m, x-=y) => 由扩展Euclid求解gcd(a,b)=ax+by 进而求得最小且不小于0的t
 Author: CriusWuBlog: http://kingpro.cnblogs.comContact: kingpro@live.cn
Author: CriusWuBlog: http://kingpro.cnblogs.comContact: kingpro@live.cn
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
 
                    
                 
 posted on
 posted on 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号