H_On个人小站

个人站

这都被你发现了【惊
奖励你一朵小红花~


【Codeforces Round#620 (Div. 2)】A. Two Rabbits 题解

题目链接:A. Two Rabbits

题目

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.

输入

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≤10^9) — 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.

The customers are given in non-decreasing order of their visit time, and the current time is 0.

输出

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.

样例

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

题意

已经知道两只兔子的初始位置,两只兔子对着跳,问你两只兔子能不能刚好碰面。

输出碰面所需要的时间,如果不能刚好碰面输出 -1 。

解题思路

因为 a 🐇往右跳,b 🐇往左跳,所以如果一开始 a 就在 b 的右边,直接 -1 。
如果 a,b 之间的距离和不能被 a,b 各自一跳的距离的和整除,那么直接 -1 。能整除就输出除数。

代码

#include <iostream>

using namespace std;

int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        int t; cin >> t;
        while (t--) {
                int a, b, c, d; cin >> a >> b >> c >> d;
                if (a > b || (b - a) % (c + d)) cout << "-1" << endl;
                else cout << (b - a)/(c + d) << endl;
        }
        return 0;
}