キャディプログラミングコンテスト2021(AtCoder Beginner Contest 193)
E
Oversleeping
AtCoder
⌀ 1948
原题
← DF →
翻译中…

Problem Statement

翻译中…

A train goes back and forth between Town $A$ and Town $B$. It departs Town $A$ at time $0$ and then repeats the following:

翻译中…
  • goes to Town $B$, taking $X$ seconds;
  • stops at Town $B$ for $Y$ seconds;
  • goes to Town $A$, taking $X$ seconds;
  • stops at Town $A$ for $Y$ seconds.
翻译中…

More formally, these intervals of time are half-open, that is, for each $n = 0, 1, 2, \dots$:

翻译中…
  • at time $t$ such that $(2X + 2Y)n ≤ t < (2X + 2Y)n + X$, the train is going to Town $B$;
  • at time $t$ such that $(2X + 2Y)n + X ≤ t < (2X + 2Y)n + X + Y$, the train is stopping at Town $B$;
  • at time $t$ such that $(2X + 2Y)n + X + Y ≤ t < (2X + 2Y)n + 2X + Y$, the train is going to Town $A$;
  • at time $t$ such that $(2X + 2Y)n + 2X + Y ≤ t < (2X + 2Y)(n + 1)$, the train is stopping at Town $A$.
翻译中…

Takahashi is thinking of taking this train to depart Town $A$ at time $0$ and getting off at Town $B$. After the departure, he will repeat the following:

翻译中…
  • be asleep for $P$ seconds;
  • be awake for $Q$ seconds.
翻译中…

Again, these intervals of time are half-open, that is, for each $n = 0, 1, 2, \dots$:

翻译中…
  • at time $t$ such that $(P + Q)n ≤ t < (P + Q)n + P$, Takahashi is asleep;
  • at time $t$ such that $(P + Q)n + P ≤ t < (P + Q)(n + 1)$, Takahashi is awake.
翻译中…

He can get off the train at Town $B$ if it is stopping at Town $B$ and he is awake. Determine whether he can get off at Town $B$. If he can, find the earliest possible time to do so. Under the constraints of this problem, it can be proved that the earliest time is an integer.

翻译中…

You are given $T$ cases. Solve each of them.

翻译中…

Constraints

翻译中…
  • All values in input are integers.
  • $1 ≤ T ≤ 10$
  • $1 ≤ X ≤ 10^9$
  • $1 ≤ Y ≤ 500$
  • $1 ≤ P ≤ 10^9$
  • $1 ≤ Q ≤ 500$
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

T
\rm case_1
\rm case_2
\hspace{9pt}\vdots
\rm case_T

翻译中…

Each case is in the following format:

翻译中…

X Y P Q

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line should contain the answer to $\rm case_i$. If there exists a time such that Takahashi can get off the train at Town $B$, the line should contain the earliest such time; otherwise, the line should contain infinity.

翻译中…

样例

Input #1
3
5 2 7 6
1 1 3 1
999999999 1 1000000000 1
Output #1
20
infinity
1000000000999999999
Let $[a, b)$ denote the interval $a ≤ t < b$. In the first case, the train stops at Town $B$ during $[5, 7), [19, 21), [33, 35), \dots$, and Takahashi is awake during $[7, 13), [20, 26), [33, 39), \dots$, so he can get off at time $20$ at the earliest.
题目信息
题号
abc193_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。