#Q215. 「一本通 6.4 例 5」Strange Way to Express Integers

    ID: 2297 Type: Default 1000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>扩展欧几里德算法中国剩余定理一本通提高

「一本通 6.4 例 5」Strange Way to Express Integers

Description

Original source: POJ 2891

Given 2n2n positive integers a1,a2,,ana_1,a_2,\cdots ,a_n and m1,m2,,mnm_1,m_2,\cdots ,m_n, find the smallest positive integer xx such that i[1,n],xai (modmi )\forall i\in[1,n],x\equiv a_i\ (\bmod m_i\ ), or determine that no solution exists.

Input Format

Multiple test cases.

For each test case, the first line contains an integer nn;
The next nn lines each contain two integers mi,aim_i,a_i.

Output Format

For each test case, if no solution exists, output 1-1; otherwise, output a non-negative integer. If there are multiple solutions, output the smallest valid answer.

Sample 1

2
8 7
11 9

31

Data Range and Hints

For all data, all inputs are non-negative and can be represented by a 64-bit signed integer. It is guaranteed that 1n1051\le n\le 10^5 and mi>aim_i\gt a_i.