Periodical Cicadas
InterviewTime limit1sMemory limit1024 MB
For each pair of cicadas with cycles c1 and c2 last seen together in year y, find the earliest year any pair reappears together, which is y plus lcm(c1, c2).
- Level
Easy3 of 10
- Topics
- Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
There are two different species of periodical cicadas that appear from hibernation every and years, respectively. Your old grandpa tells you that he saw them simultaneously back in '92. You start pondering how many years you have to wait until you see them again. You collect information about other pairs of periodical cicadas and when they were last observed to find out when the next simultaneous appearance is.
Given several different pairs of cicadas and their last simultaneous appearance, find the next year that one of the pairs reappears.
Input
The first line of input contains a single integer (), the number of pairs of periodical cicadas. Then follow lines, each containing three integers , and (, ), the year this pair was last observed and cycle lengths for the first and second species, respectively. You may assume that none of the pairs reappears earlier than .
Output
Output the first year a pair reappears.