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

#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;
}





				
时间: 2024-08-30 14:55:48

poj 2891(中国剩余定理)的相关文章

poj 2891 中国剩余定理

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

POJ 1006 中国剩余定理

[题意]: 给定p,e,i,d,求解 (x + d) % 23 = p (x + d) % 28 = e(x + d) % 33 = i x最小正整数值 [知识点]: 中国剩余定理 [题解]: 典型的 xmodmi = ai模型,其中mi间两两互素.但该题式子较少,也可以直接自己化简带入值. [代码]: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #inc

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

Biorhythms POJ - 1006 中国剩余定理

定理证明:https://blog.csdn.net/d_x_d/article/details/48466957 https://blog.csdn.net/lyy289065406/article/details/6648551 关键思路在于构造一个每部分膜另外一个不包含在部分内的部分的余数是1  然后把各部分分别乘以对应的余数 其中题目给出的第一天天数  就是  (n+d)%xn 的余数 不用处理 1 2 #include<iostream> 3 using namespace std;

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

acm数论之旅--中国剩余定理

中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这个答案 5*7*inv(5*7,  3) % 3  =  1 3*7*inv(3*7,  5) % 5  =  1 3*5*inv(3*5,  7) % 7  =  1 这3个式子对不对,别告诉我逆元你忘了(*′?`*),忘了的人请翻阅前几章复习 然后两边同乘你需要的数 2 * 5*7*inv(5*

poj 1006(剩余定理)

生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111426   Accepted: 34702 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod a[i] = b[i], - (0 < a[i] <= 10). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include