poj 2891(中国剩余定理)

Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 10524 Accepted: 3194

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 a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, 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 airi (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
题意:给你k组数据  接下来是ai  ri  问你是否存在一个数x  满足x%ai=ri  若存在输出最小的一个  否则输出-1;
分析:

先考虑k==2的情况:

x = a1 ( mod m1 )

x = a2 ( mod m2 )

方程组有解的充分必要条件是: d | (a1-a2) ,其中 d = (m1,m2)

证明如下:

必要性: 设 x 是上面同余方程组的解,从而存在整数q1,q2使得x=a1+m1*q1,x=a2+m2*q2,消去x即得a1-a2 = m2q2-m1q1。由于d=(m1,m2),故d | (a1-a2)。

充分性:若d=(m1,m2) | (a1-a2)成立,则方程m1*x + m2*y = a1-a2有解。

设解为x0,y0。那么m2*y0 = a1-a2 ( mod m1 )

记x1 = a2+m2*y0,可以知道 x1=a2 ( mod m2 ),且x1 = a2+m2*y0 = a2 + ( a1-a2) = a1 ( mod m1 )

所以 x1 = a2 ( mod m2 ) = a1 ( mod m1 ) 

所以 x = x1 ( mod [m1,m2] ) 

另外,若x1与x2都是上面同余方程组的解,则 x1 = x2 ( mod m1 ), x1 = x2 ( mod m2 ), 由同余的性质得 x1 = x2 ( mod [m1,m2] ),即对于模[m1,m2],同余方程组的解释唯一的。

PS:由于ai,aj不保证互素,不能用直接套中国剩余定理,做法是利用欧几里德扩展定理,将两个等式合并,然后再与其他的等式一一合并

更详细的解释请参考算法导论p556

一位大牛的解释:由于这道题目里面的ai、ri之间不满足两两互质的性质,所以不能用中国剩余定理直接求解。
不过,我们可以模仿中国剩余定理的做法来解决这个问题。
如果只有一个方程:x mod a0 = r0。那么,显然x的最小正值为a0+r0。
根据模的性质,我们容易得知,x+a0*k均为该方程的解。(k为正整数)
如果多了一个方程:x mod a1 = r1。那么,我们为了使之间求得的解x0=a0+r0能够同时满足这两个方程,只好令x0=x0+a0*k,显然这样做x0仍然满足第一个方程。这时候我 们相当于要求解这样一个模方程:(x0+a0*k) mod a1 = r1。这个方程我们可以用拓展欧几里得算法求得k的值。这样,只要令x0变成x0+a0*k,就能同时满足这两个方程了。
推而广之,对于方程x mod ai = ri,假如我们之前求得的解为X,那么我们要令X变成X+k*LCM(a0,a1,a2...ai-1),使得它满足这个方程。k我们可以用拓展欧几里得 算法求解,LCM可以在每一次更新,这样就能在接近O(klogk)的时间复杂度内解决这个问题了。
无解的判断:若某个(X+k*LCM) mod ai = ri无整数解,那么原方程组无解。
该题虽然数据规模较大,但比较仁慈,所有ai的LCM是64位整型,所以不用使用高精度。

 
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <limits.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define N 100010
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
#define LL long long
using namespace std;
LL k,m,t;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return (b,a%b);
}
LL extend(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL d=extend(b,a%b,x,y);
    t=x;
    x=y;
    y=t-a/b*y;
    return d;
}
int main()
{
    int i,flag;
    LL p,q,d,a1,a2,b1,b2,x,c;
    while(scanf("%lld",&k)!=EOF)
    {
        scanf("%lld%lld",&b1,&a1);
        flag=0;
        for(i=0; i<k-1; i++)
        {
            scanf("%lld%lld",&b2,&a2);
            if(flag)
                continue;
            d=extend(b1,b2,p,q);
            c=a2-a1;
            if(c%d)
            {
                flag=1;
                continue;
            }
            t=b2/d;
            x=(c/d*p%b2+b2)%t;
            a1=x*b1+a1;
            b1=b1*b2/d;
        }
        if(flag)
            puts("-1");
        else
            printf("%lld\n",a1);
    }
    return 0;
}


 
 
 
 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值