Codeforces Round #620 (Div. 2):A. Two Rabbits

Discription
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.

He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position x, and the shorter rabbit is currently on position y (x<y). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by a, and the shorter rabbit hops to the negative direction by b.
在这里插入图片描述

For example, let’s say x=0, y=10, a=2, and b=3. At the 1-st second, each rabbit will be at position 2 and 7. At the 2-nd second, both rabbits will be at position 4.

Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let’s find a moment in time (in seconds) after which the rabbits will be at the same point.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000).

Each test case contains exactly one line. The line consists of four integers x, y, a, b (0≤x<y≤109, 1≤a,b≤109) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

Output
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.

If the two rabbits will never be at the same position simultaneously, print −1.

Example
Input

5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1

output

2
-1
10
-1
1

Note
The first case is explained in the description.

In the second case, each rabbit will be at position 3 and 7 respectively at the 1-st second. But in the 2-nd second they will be at 6 and 4 respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.

题意
有两只兔子同时跳,第一只兔子在x点,每次只能往右跳a格,第二只兔子在y点,每次只能往左跳b格。
问这两只兔子能否碰头,如果能,碰头时间时多少。

思路
两只兔子距离为(y-x),每次缩小的距离为(a+b),如果(y-x)<0的话,则不可以。如果(y-x)能整除(a+b),除数就是次数,则代表可以碰头,否则不可以。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define rep(i, a, n) for(int i=a; i<=n; i++)
#define per(i, n, a) for(int i=n; i>=a; i--)
 
int T;
ll x,y,a,b;
 
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        sldd(x,y);
        sldd(a,b);
        if(x>y)
        {
            printf("-1\n");
            continue;
        }
        ll m=(y-x)/(a+b);
        if(m*(a+b)==(y-x))
        {
            printf("%lld\n",m);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}
发布了313 篇原创文章 · 获赞 105 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43460224/article/details/104348204