c语言sscanf函数的用法是什么
247
2022-09-02
poj 2891 Strange Way to Express Integers 扩展CRT
Description
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
Line 1: Contains the integer k. Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k). Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2 8 7 11 9 Sample Output
31 Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
Source
POJ Monthly–2006.07.30, Static 那么考虑当两个余数不互质的时候 我们应该如何处理 假设他们分别是m1 c1 m2 c2 那么转换下形式 x=k1∗m1+c1 x = k 1 ∗ m 1 + c 1 x=k2∗m2+c2 x = k 2 ∗ m 2 + c 2 满足翡蜀定理 即gcd(m1,m2)|(c1−c2) g c d ( m 1 , m 2 ) | ( c 1 − c 2 ) 这种情况下才有解 k1∗m1=k2∗m2−c1+c2 k 1 ∗ m 1 = k 2 ∗ m 2 − c 1 + c 2 两个同除gcd(m1,m2) m1(m1,m2)k1=k2m2(m1,m2)+c2−c1(m1,m2) m 1 ( m 1 , m 2 ) k 1 = k 2 m 2 ( m 1 , m 2 ) + c 2 − c 1 ( m 1 , m 2 ) m1(m1,m2)k1≡c2−c1(m1,m2)(modm2(m1,m2)) m 1 ( m 1 , m 2 ) k 1 ≡ c 2 − c 1 ( m 1 , m 2 ) ( m o d m 2 ( m 1 , m 2 ) ) 考虑将左边的系数除到右边 k1≡inv(m1(m1,m2),m2(m1,m2))×(c2−c1(m1,m2))(modm2(m1,m2)) k 1 ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) ( m o d m 2 ( m 1 , m 2 ) ) 再将原来的系数还原回来 k1=inv(m1(m1,m2),m2(m1,m2))×(c2−c1(m1,m2))+(y×m2(m1,m2)) k 1 = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) + ( y × m 2 ( m 1 , m 2 ) ) 然后再将k1带回原式可以发现 x≡inv(m1(m1,m2),m2(m1,m2))×(c2−c1(m1,m2))×m1+c1(modm1×m2(m1,m2)) x ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) × m 1 + c 1 ( m o d m 1 × m 2 ( m 1 , m 2 ) ) 然后这题就搞定了 那么本题只需要直接exgcd求出的即是我想要的 inv(m1(m1,m2)) i n v ( m 1 ( m 1 , m 2 ) )
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~