poj 2891 Strange Way to Express Integers 中國剩餘定理

來源:互聯網
上載者:User
Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 4191   Accepted: 1100

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

28 711 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

 

/*<br />對於w[1],w[2].....w[n]不互素的情形,就只能兩個兩個來求了<br />x=a[1] (mod m[1])<br />x=a[2] (mod m[2])<br />解完後,a=x,m=m1和m2的最小公倍數<br />將題目意思轉化為公式:a1*x-a2*y=r2-r1,用歐幾裡得擴充演算法求解</p><p>a1+m1*y=a2+m2*z ==> a1-a2=m2*z-m1*y<br />則(a1-a2)%gcd(m1,m2)=0<br />則:x=a2+m2*z (1)<br />(應該a>m,必需條件)<br />x=r1 (mod a1)<br />x=r2 (mod a2)<br />---> //a1*x+a2*y=gcd(a1,a2)=d<br />---> //r1+a1*x=r2+a2*y ---> a1*x+a2*y=r2-r1<br />則(r2-r1)%d!=0 不能得出<br />否則<br />{<br /> t=a2/d;<br /> x=((x*(r2-r1)/d)%t+t)%t//x的最小整數解<br /> r1=x*a1+r1 //由(1)式<br /> a1=a1*a2/d;<br /> r1=(r1%a1+a1)%a1;<br />}<br />*/<br />#include<cstdio><br />#include<cstdlib><br />long long int ex_Gcd(long long int p,long long int q,long long int &x,long long &y)<br />{<br /> long long int temp,r;<br /> if(q==0)<br /> {<br /> x=1;<br /> y=0;<br /> return p;<br /> }<br /> r=ex_Gcd(q,p%q,x,y);<br /> temp=x;<br /> x=y;<br /> y=temp-p/q*y;<br /> return r;<br />}<br />int main()<br />{<br /> long long int n,a1,a2,r1,r2,x,y,i,d,t;<br /> bool flag;<br /> while(scanf("%lld",&n)!=EOF)<br /> {<br /> scanf("%lld %lld",&a1,&r1);<br /> if(n==1)<br /> {<br /> if(a1<=r1) //餘數不可能大於除數<br /> printf("-1/n");<br /> else<br /> printf("%lld/n",r1);<br /> continue;<br /> }<br /> flag=true;<br /> for(i=1; i<n; i++)<br /> {<br /> scanf("%lld %lld",&a2,&r2);<br /> if(!flag) continue;<br /> if(a2<=r2)<br /> flag=false;<br /> d=ex_Gcd(a1,a2,x,y);//a1*x+a2*y=gcd(a1,a2)<br /> if((r2-r1)%d!=0) flag=false;<br /> else<br /> {<br /> t=a2/d;<br /> x=((r2-r1)/d*x%t+t)%t;<br /> r1=x*a1+r1;<br /> a1=a1*a2/d; //a1為a1和a2的最小公倍數<br /> r1=(r1%a1+a1)%a1;<br /> }<br /> }</p><p> if(flag)<br /> printf("%lld/n",r1);<br /> else<br /> printf("-1/n");<br /> }<br /> return 0;<br />}<br /> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.