标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 108835 | Accepted: 21851 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
Source
分析:
假设跳了 T 次以后,青蛙 1 的坐标便是 x+m*T,青蛙 2 的坐标为 y+n*T。它们能够相遇
的情况为(x+m*T)-(y+n*T)==P*L,其中 P 为某一个整数,变形一下
得到(n-m)*T-P*L==x-y 我们设 a=(n-m),b=L,c=x-y,T=x,P=y.于是便得到 ax+by==c,直
接利用欧几里得扩展定理可以得到一组 x,y 但是这组 x,y 不一定是符合条件的最优解,首
先,当 gcd(a,b)不能整除 c 的时候是无解的,当 c 能整除 gcd(a,b)时,把 x 和 y 都要变为原来
的 c/gcd(a,b)倍,我们知道它的通解为 x0+b/gcd(a,b)*t 要保证这个解是不小于零的最小的
数,我们先计算当 x0+b/gcd(a,b)*t=0 时的 t 值,此时的 t 记为 t0=-x0/b/gcd(a,b)(整数除
法) ,代入 t0 如果得到的 x 小于零再加上一个 b/gcd(a,b)就可以了。
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <iostream> 9 #include "algorithm" 10 using namespace std; 11 typedef long long LL; 12 LL _x,_y,n,m,l; 13 LL a,b,c,d; 14 LL x,y; 15 LL exgcd(LL a,LL b,LL &x,LL &y){ 16 if (b==0) 17 {x=1; 18 y=0; 19 return a; 20 } 21 LL d=exgcd(b,a%b,x,y); 22 LL t=x; 23 x=y; 24 y=t-a/b*y; 25 return d; 26 } 27 int main(){ 28 freopen ("frog.in","r",stdin); 29 freopen ("frog.out","w",stdout); 30 int i,j; 31 while (scanf("%lld%lld%lld%lld%lld",&_x,&_y,&m,&n,&l)!=EOF){ 32 a=n-m,b=l,c=_x-_y; 33 d=exgcd(a,b,x,y); 34 if (c%d!=0) 35 {puts("Impossible"); 36 continue; 37 } 38 x=x*(c/d),y=y*(c/d); 39 LL t; 40 t=-x*d/b; 41 t=x+t*b/d; 42 if (t<0) 43 t+=b/d; 44 printf("%lld",t); 45 } 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/keximeiruguo/p/5971714.html