#GL0000278. Quái vật truy đuổi (The Monster)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Quái vật truy đuổi (The Monster)
The Monster
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Rick screams at times , while Morty screams at . All four parameters are positive integers. Find the earliest time that belongs to both sequences, or print -1 if none exists.
Input
Line 1: integers , Rick's interval and first scream time.
Line 2: integers , Morty's corresponding parameters.
Output
One integer: the earliest common time, or -1 if it does not exist.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Example 1
Input
20 2
9 19
Output
82
Explanation
Rick starts at 2 and repeats every 20; Morty starts at 19 and repeats every 9. The earliest common time is 82, satisfying both progressions.
Example 2
Input
2 1
16 12
Output
-1
Explanation
Rick starts at 1 and repeats every 2; Morty starts at 12 and repeats every 16. The start-time difference 11 is not divisible by gcd(2,16)=2, so no common time exists.