【POJ 2891 Strange Way to Express Integers】+ 扩展欧几里德
2016-11-15 19:29
429 查看
Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 14741 Accepted: 4827
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.
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
题意 : 输入方程组X%a[i] = r[i],求X
思路 : 扩展欧几里德~
AC代码 :
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 14741 Accepted: 4827
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
题意 : 输入方程组X%a[i] = r[i],求X
思路 : 扩展欧几里德~
AC代码 :
#include<cstdio> typedef long long LL; LL EXGCD(LL a,LL b,LL &x,LL &y){ if(!b){ x = 1; y = 0; return a; } LL ans = EXGCD(b,a % b,y,x); y -= a / b * x; return ans; } int main() { LL n,a,b,c1,c2,x,y,v; while(scanf("%lld",&n) != EOF){ scanf("%lld %lld",&a,&c1); bool ok = false; for(int i = 1 ; i < n ; i++){ scanf("%lld %lld",&b,&c2); if(ok) continue; LL d = EXGCD(a,b,x,y),c = c2 - c1; if(c % d){ ok = true; continue; } c /= d; v = b / d; x = ((x * c) % v + v) % v; c1 += a * x; a = a / d * b; c1 %= a; } if(ok) printf("-1\n"); else printf("%lld\n",c1); } return 0; }
相关文章推荐
- POJ 2891-Strange Way to Express Integers(扩展欧几里德)
- POJ 2891 Strange Way to Express Integers ★ (扩展欧几里德解同余式组)
- POJ 2891 Strange Way to Express Integers ★ (扩展欧几里德解同余式组)
- poj 2891 Strange Way to Express Integers (扩展gcd)
- poj-2891-Strange Way to Express Integers【扩展欧几里得】
- poj 2891 Strange Way to Express Integers(扩展欧几里得,中国剩余定理)
- POJ 2891 Strange Way to Express Integers(中国剩余定理扩展)
- POJ Strange Way to Express Integers 2891(扩展欧几里得)
- POJ 2891 Strange Way to Express Integers[中国剩余定理(非互质)][扩展欧几里得]
- POJ-----2891---Strange Way to Express Integers扩展欧几里得
- poj-2891(Strange Way to Express Integers)--中国剩余定理&&扩展欧几里得
- poj2891——Strange Way to Express Integers(扩展欧几里得解中国剩余定理)
- POJ 2891 Strange Way to Express Integers(扩展欧几里得)
- POJ-2891 Strange Way to Express Integers (数论:扩展欧几米德定理)
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- 【POJ 2891】Strange Way to Express Integers(扩展欧几里得)
- POJ 2891 Strange Way to Express Integers 扩展gcd
- POJ 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- poj 2891 Strange Way to Express Integers(解一般模线性方程组)