#P1495. 【模板】中国剩余定理(CRT)/ 曹冲养猪

【模板】中国剩余定理(CRT)/ 曹冲养猪

Description

After Cao Chong solved the elephant problem, Cao Cao wanted his son to take on some real work, so he sent him to manage a pig farm in the Central Plains. Cao Chong was unhappy and worked carelessly. One day, Cao Cao wanted to know the number of sows, so Cao Chong decided to play a trick on him. For example, suppose there are 1616 sows. If 33 pens are built, then 11 sow has nowhere to go. If 55 pens are built, there is still 11 sow with no place to go. If 77 pens are built, there are 22 sows left without a place. As Cao Cao’s personal secretary, you must report the exact number of sows. What should you do?

Input Format

The first line contains an integer nn — the number of times pens are built. The next nn lines each contain two integers ai,bia_i, b_i, meaning that when aia_i pens are built, there are bib_i sows without a place to go. You may assume a1ana_1 \sim a_n are pairwise coprime.

Output Format

Output a single positive integer: the minimum possible number of sows that Cao Chong raises.

3
3 1
5 1
7 2
16

Hint

1n101 \leq n\le100bi<ai1000000 \leq b_i\lt a_i\le1000001ai10181 \leq \prod a_i \leq 10^{18}

Translated by ChatGPT 5