Strange Way to Express Integers(中國剩餘定理+不互質),strangeintegers

來源:互聯網
上載者:User

Strange Way to Express Integers(中國剩餘定理+不互質),strangeintegers
Strange Way to Express IntegersTime Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64uSubmit Status Practice POJ 2891Appoint description: System Crawler  (2015-04-27)

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.

 

 

 

 

 

 

 

 

 

 

 

題意:給你k組數。x%M[i]=A[i];

 

思路:中國剩餘定理,擴充歐幾裡德

 

不會的可以參考:http://blog.csdn.net/u010579068/article/details/45422941

 

轉載請註明出處:http://blog.csdn.net/u010579068

 

題目連結:http://poj.org/problem?id=2891

 

 

 

 

#include<stdio.h>#define LL __int64void exgcd(LL a,LL b,LL& d,LL& x,LL& y){    if(!b){d=a;x=1;y=0;}    else    {        exgcd(b,a%b,d,y,x);        y-=x*(a/b);    }}LL gcd(LL a,LL b){    if(!b){return a;}    gcd(b,a%b);}LL M[55000],A[55000];LL China(int r){    LL dm,i,a,b,x,y,d;    LL c,c1,c2;    a=M[0];    c1=A[0];    for(i=1; i<r; i++)    {        b=M[i];        c2=A[i];        exgcd(a,b,d,x,y);        c=c2-c1;        if(c%d) return -1;//c一定是d的倍數,如果不是,則,肯定無解        dm=b/d;        x=((x*(c/d))%dm+dm)%dm;//保證x為最小正數//c/dm是餘數,係數擴大庾數被        c1=a*x+c1;        a=a*dm;    }    if(c1==0)//餘數為0,說明M[]是等比數列。且餘數都為0    {        c1=1;        for(i=0;i<r;i++)            c1=c1*M[i]/gcd(c1,M[i]);    }    return c1;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<n;i++)        {            scanf("%I64d%I64d",&M[i],&A[i]);        }        if(n==1){ printf("%I64d\n",A[0]);continue;}        LL ans=China(n);        printf("%I64d\n",ans);    }    return 0;}


 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.