Bajtemon Collector 2
Time limit4sMemory limit1024 MB
Each card contributes either its power before or after evolution. Find the largest possible GCD of the chosen powers.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Brute force
- Solved
- No attempts yet
Problem
Bajtazar has a huge collection of Bajtemon cards. Each card shows a Bajtemon and lists its power before and after evolution. Bajtazar wants to sell the collection. Under the Bajtocko-Bitocki Agreement on Bajtemon Card Trading, the price of a collection is proportional to the greatest common divisor (GCD) of the powers of all Bajtemons in the deck. The agreement did not account for cards with two powers, so for each card Bajtazar may choose whether to count the power before or after evolution. He wants the GCD to be as large as possible. Write a program that finds the maximum price at which he can sell his collection.
Input
The first line contains an integer (), the number of cards. Each of the next lines contains two integers and (, ), separated by a single space. They are the power of the i-th Bajtemon before and after evolution. The power after evolution may be smaller than the power before evolution.
Output
Print one integer, the maximum possible GCD of the Bajtemon powers that Bajtazar can obtain.
Hint
In the example, Bajtazar picks the powers after evolution for the third and fourth Bajtemons, and the powers before evolution for the first and second. The resulting powers are 5, 10, 20, and 5, so their GCD is 5.