poj 2891 中国剩余定理 线性同余方程

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-negativem, divide it by everyai (1 ≤ik) to find the remainderri. Ifa1,a2, …,ak are properly chosen, m can be determined, then the pairs (ai,ri) can be used to expressm.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I findm 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 ≤ik).
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

 转载

/**********************一般模线性方程组***********************/

同样是求这个东西。。
X mod m1=r1
X mod m2=r2
...
...
...
X mod mn=rn

首先,我们看两个式子的情况
X mod m1=r1……………………………………………………………(1)
X mod m2=r2……………………………………………………………(2)
则有 
X=m1*k1+r1………………………………………………………………(*)
X=m2*k2+r2
那么 m1*k1+r1=m2*k2+r2
整理,得
m1*k1-m2*k2=r2-r1
令(a,b,x,y,m)=(m1,m2,k1,k2,r2-r1),原式变成
ax+by=m
熟悉吧?

此时,因为GCD(a,b)=1不一定成立,GCD(a,b) | m 也就不一定成立。所以应该先判 若 GCD(a,b) | m 不成立,则!!!方程无解!!!。
否则,继续往下。

解出(x,y),将k1=x反代回(*),得到X。
于是X就是这两个方程的一个特解,通解就是 X'=X+k*LCM(m1,m2)
这个式子再一变形,得 X' mod LCM(m1,m2)=X
这个方程一出来,说明我们实现了(1)(2)两个方程的合并。
令 M=LCM(m1,m2),R=r2-r1
就可将合并后的方程记为 X mod M = R。

然后,扩展到n个方程。
用合并后的方程再来和其他的方程按这样的方式进行合并,最后就能只剩下一个方程 X mod M=R,其中 M=LCM(m1,m2,...,mn)。
那么,X便是原模线性方程组的一个特解,通解为 X'=X+k*M。

如果,要得到X的最小正整数解,就还是原来那个方法:


#include<cstdio>
using namespace std;
typedef long long ll;
const int M=1000000;
ll m[M],r[M];
int t;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    ll d=a;
    if(b==0)
    {
        x=1;y=0;
    }
    else
    {
        d=exgcd(b,a%b,y,x);
        y-= (a/b)*x;
    }
    return d;
}
ll solve()
{
    ll a=m[1],b=r[1],x,y,d,c;
    for(int i=2;i<=t;i++)
    {
        d=exgcd(a,m[i],x,y);
        c=r[i]-b;
        if(c%d)
            return -1;
        x=c/d*x%(m[i]/d);//通解为x=x0+kb/n=x0%(b/n)
        b+=x*a;//特解x0,新的余数
        a=a/d*m[i];//新的模数  
        b%=a;
    }
    return b>0?b:b+a;
}
int main()
{
   while(~scanf("%d",&t))
    {
        for(int i=1;i<=t;i++)
            scanf("%lld%lld",&m[i],&r[i]);
        printf("%lld\n",solve());
    }
    return 0;
}
/*   x≡2(mod3)
     x≡3(mod5)
     这两个得b=8(x得一个解),a=15(3*5/gcd(3,5))
     x≡2(mod7)
     b=8-6*15,a=15*7/gcd(15,7) b=((b%a)+a)%a=23  (b为最小正整数得x)
https://wenku.baidu.com/view/bfe833ca700abb68a882fb24.html
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值