#GL0000309. Ngủ quên (Oversleeping)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ngủ quên (Oversleeping)
Oversleeping
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A train repeatedly travels between towns and . At time it leaves , then travels to for seconds, stops at for seconds, travels back to for seconds, and stops at for seconds.
A passenger boards the train at time . The passenger repeatedly sleeps for seconds and stays awake for seconds, starting asleep. The passenger may get off only when the train is stopped at and the passenger is awake.
All time intervals are half-open: the left endpoint is included and the right endpoint is excluded. For integer , the train stops at during . For integer , the passenger is awake during .
Find the earliest time when the passenger can get off at , or print infinity if this can never happen. When the answer exists, the earliest such time is guaranteed to be an integer.
Input
One line contains four positive integers in this order.
Output
Print the earliest integer time when getting off at is possible, or exactly infinity if it is impossible.
Subtasks
- Subtask 1 (20 points): ; ; all inputs are integers.
- Subtask 2 (30 points): ; ; all inputs are integers.
- Subtask 3 (50 points): ; ; all inputs are integers.
Examples
Example 1
Input
5 2 7 6
Output
20
Explanation
The train stops at during , , , etc. The passenger is awake during , , , etc. The first overlap is at , which belongs to both and . At the passenger is asleep, so the earliest time is .
Example 2
Input
1 1 3 1
Output
infinity
Explanation
The train is stopped at only at integer times , while the passenger is awake only at . These sets do not intersect, so getting off is impossible.
Example 3
Input
999999999 1 1000000000 1
Output
1000000000999999999
Explanation
The train period is , with its one-second stop at beginning at remainder . The sleep/wake period is , with wakefulness at remainder . The smallest time meeting both congruences is .