description:
-
有一条数轴,上面有两只兔子。
-
一只兔子位于 x x x 坐标,每秒跳 a a a 个单位,另一只位于 y y y 坐标,每秒跳 b b b 个单位,保证 x < y x<y x<y ,两只兔子相向而行。
-
请你求出两只兔子是否会在整数秒相遇,如果是,打印出相遇的那一秒(初始时间从 0 0 0 开始),如果无法在整数秒相遇,则输出
-1
。 -
translate by @ShineEternal。
solution:
我们发现兔子一定会相遇,只是可能不在整数秒,根据相遇问题的公式,我们只需要判断 ( y − x ) m o d ( a + b ) (y-x)\bmod (a+b) (y−x)mod(a+b) 是否为 0 0 0 即可。(即是否为整数秒)
code:
#include<cstdio>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
long long x,y,a,b;
scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
long long z=y-x;
long long c=a+b;
if(z%c==0)
{
printf("%d\n",z/c);
}
else
{
printf("-1\n");
}
}
return 0;
}