#P4777. 【模板】扩展中国剩余定理(EXCRT)

    ID: 3563 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>最大公约数,gcd中国剩余定理,CRT

【模板】扩展中国剩余定理(EXCRT)

Description

Given nn pairs of non-negative integers ai,bia_i, b_i, find the smallest non-negative integer solution to the system of equations in xx.

$$\begin{cases}x\equiv b_1\pmod{a_1}\\x\equiv b_2\pmod{a_2}\\\dots\\x\equiv b_n\pmod{a_n}\end{cases}$$

Input Format

The first line contains an integer nn.

The next nn lines each contain two non-negative integers ai,bia_i, b_i.

Output Format

Output one line: the smallest non-negative integer xx that satisfies the conditions.

3
11 6
25 9
33 17

809

Hint

For 100%100\% of the testdata, 1n1051 \le n \le {10}^5, 1ai10121 \le a_i \le {10}^{12}, 0bi10120\leq b_i \leq 10^{12}. It is guaranteed that the least common multiple of all aia_i does not exceed 1018{10}^{18}.

Please note that during the execution of the program, multiplication operations may overflow.

It is guaranteed that a solution exists.

Translated by ChatGPT 5