[POJ 2891] Strange Way to Express Integers
2015-04-19 23:02
295 查看
Strange Way to Express Integers
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
Sample Output
中国剩余定理、
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 10907 | Accepted: 3336 |
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
中国剩余定理、
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define ll long long #define N 10010 ll exgcd(ll a,ll b,ll& x, ll& y) { if(b==0) { x=1; y=0; return a; } ll d=exgcd(b,a%b,y,x); y-=a/b*x; return d; } bool solve(ll &m0,ll &a0,ll m,ll a) { ll y,x; ll g=exgcd(m0,m,x,y); if((a-a0)%g) return 0; x*=(a-a0)/g; x%=m/g; a0=(x*m0+a0); m0*=m/g; a0%=m0; if(a0<0) a0+=m0; return 1; } bool MLES(ll m[],ll r[],ll &m0 ,ll &a0,ll n) { m0=1; a0=0; bool flag=1; for(ll i=0;i<n;i++) { if(!solve(m0,a0,m[i],r[i])) { flag=0; break; } } return flag; } int main() { ll n; ll m ,r ; while(scanf("%lld",&n)!=EOF) { for(ll i=0;i<n;i++) { scanf("%lld%lld",&m[i],&r[i]); } ll m0,a0; ll flag=MLES(m,r,m0,a0,n); if(flag) printf("%lld\n",a0); else printf("-1\n"); } return 0; }
相关文章推荐
- POJ_2891_Strange Way to Express Integers(非互质的中国剩余定理)
- [poj] 2891 Strange Way to Express Integers
- lightoj 1319 - Monkey Tradition 中国剩余定理(孙子定理)+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( EXGCD)
- 【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解线性同余方程组(中国剩余定理不互质版)
- 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【crt】
- POJ——T 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 中国剩余定理MOD不互质数字方法
- poj-2891-Strange Way to Express Integers【扩展欧几里得】