poj 2891 Strange Way to Express Integers(中国剩余定理)

http://poj.org/problem?id=2891

题意:求解一个数x使得 x%8 = 7,x%11 = 9;
   若x存在,输出最小整数解。否则输出-1;

ps:

思路:这不是简单的中国剩余定理问题,由于输入的ai不一定两两互质,而中国剩余定理的条件是除数两两互质。
   这是一般的模线性方程组,对于
    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的最小正整数解,就还是原来那个方法:

X%=M;
if (X<0) X+=M;


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10;

_LL k;
_LL M;

_LL extend_gcd(_LL a,_LL b,_LL &x,_LL &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    else
    {
        _LL r = extend_gcd(b,a%b,x,y);
        _LL t = x;
        x = y;
        y = t-a/b*y;
        return r;
    }
}

int main()
{
    _LL a1,m1,a2,m2,x,y,i,d;
    while(scanf("%lld",&k)!= EOF)
    {
        bool flag = true;
        scanf("%lld %lld",&m1,&a1);
        for(i = 1; i < k; i++)
        {
            scanf("%lld %lld",&m2,&a2);

            d = extend_gcd(m1,m2,x,y);

            if((a2-a1)%d != 0)
                flag = false;

            _LL t = m2/d;
            x *= (a2-a1)/d;
            x = (x%t + t)%t;
            //注意新的m1,a1是怎么得来的
            a1 = x*m1+a1;
            m1 = m1*m2/d;
            a1 = (a1%m1+m1)%m1;
        }
        if(flag == true)
            printf("%lld\n",a1);
        else printf("-1\n");

    }
    return 0;
}
时间: 2024-07-30 20:27:48

poj 2891 Strange Way to Express Integers(中国剩余定理)的相关文章

poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 11970   Accepted: 3788 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express no

poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   Accepted: 2873 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

poj 2891 Strange Way to Express Integers

http://poj.org/problem?id=2891 这道题的题意是:给你多个模性方程组:m mod ai=ri 求最小的m: 中国剩余定理 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 using namespace std; 6 7 ll gcd(ll a,ll b,ll &x,ll &y) 8 { 9 if(!

poj 2891 Strange Way to Express Integers (扩展gcd)

题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2联立得:a1*x-a2*y=r2-r1;设r=r2-r2; 互质的模线性方程组m=r[i](mod a[i]).两个方程可以合并为一个,新的a1为lcm(a1,a2), 新的r为关于当前两个方程的解m,然后再和下一个方程合并--.(r2-r1)不能被gcd(a1,a2)整除时无解. 怎么推出的看了好

poj——2891 Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

poj 2891 Strange Way to Express Integers (解模线性方程组)

链接:poj 2891 题意:有一个数x,给定k组ai和ri,使得x%ai=ri 求x最小为多少 分析:求解模线性方程组 x = a1(mod m1) x = a2(mod m2) x = a3(mod m3) 先求解方程组前两项. x=m1*k1+a1=m2*k2+a2 -> m1*k1+m2*(-k2)=a2-a1 这个方程可以通过欧几里得求解出最小正整数的k1 则x=m1*k1+a1 显然x为两个方程的最小正整数解. 则这两个方程的通解为 X=x+k*LCM(m1,m2) -> X=x(

POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 15898 418373 161478614 149488440 1748022751 21618619576 810918992 241779667 1772616743 1953316358 125248280 2273149397 3849022001 2509433771 3885219405 35

poj 2891 Strange Way to Express Integers 2012-09-05

http://poj.org/problem?id=2891 解线性模方程组. 比较坑爹,数据比较大,很容易溢出. 1 Program poj2891; 2 3 var m:int64; 4 5 a,r:array[1..30000000]of int64; 6 7 ans,x,y,lcm:int64; 8 9 10 Procedure init; 11 12 var i,j:longint; 13 14 begin 15 16 m:=0; 17 18 readln(m); 19 20 for

POJ Strange Way to Express Integers [中国剩余定理]

不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; inline ll read(){ char c=getchar();ll x=0,f=1; while(c<'