poj 2891 模数不互质的中国剩余定理

Strange Way to Express Integers

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 (airi) 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

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

题意:

就是给你n组数据

每一组数据a,b   表示    x%a==b

求这n组数据的公共的最小x的解

题解:

中国剩余定理中的不互质的模线性方程组

由不互质的模线性方程组
x%r1=a1              1
x%r2=a2              2
x%r3=a3              3
x%r4=a4              4
......
由  1  2式子得到:
x = r1*k1 + a1
x = r2*k2 + a2
联立:
r1*k1 + a1 =r2*k2 + a2
得到
r1*k1 + r2*k2 = a2 - a1  (其中k2的正负可变)

令g=gcd(r1,r2)

那么上叙方程同时除以g后

r1*k1 / g + r2*k2 / g = (a2 - a1 )/ g

由扩展欧几里得可以得到k1,则有
x1 = r1*k1 + a1                         5
     x1表示1 2式子得到的r1 和 让r2的最大公约数

上ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;  

#define LL long long  

LL exgcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0){
        x=1,y=0;
        return a;
    }  

    LL d=exgcd(b,a%b,y,x);
    y-=(a/b)*x;
    return d;
}  

int main()
{
    LL k,a1,a2,r1,r2;
    LL x,y,d;
   // freopen("in.txt","r",stdin);
    while(scanf("%I64d",&k)!=EOF)
    {
        scanf("%I64d%I64d",&a1,&r1);
        k--;
        int flag=0;
        while(k--)
        {
            scanf("%I64d%I64d",&a2,&r2);
            if(flag)
                continue;  

            //扩展欧几里得
            LL a=a1,b=a2;
            LL c=r2-r1;
            d=exgcd(a,b,x,y);//最小公约数
            if(c%d){
                flag=1;
                continue;
            }  

            LL t=b/d;//除以d表示方程两边都除以d
            x=(x*(c/d)%t+t)%t;
            r1=a1*x+r1;//同上面所说的式子5
            a1=a1*(a2/d);//a1 a2最大公倍数
        }  

        if(flag)
            printf("-1\n");
        else
            printf("%I64d\n",r1);
    }
    return 0;
}  
时间: 2024-08-05 19:35:57

poj 2891 模数不互质的中国剩余定理的相关文章

hdu 3579 Hello Kiki 不互质的中国剩余定理

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一

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

hdu 1573 X问题 两两可能不互质的中国剩余定理

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 求在小于等于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). Input 输入数据的第一行为一个正整数T,

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

裸题,上模版,,嘿嘿 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<map> using namespace std; #define ll __in

拓展中国剩余定理解决模数不互质同余方程组

如果模数互质的话,直接中国剩余定理就可以了 但是如果模数不互质又没有接触这个方法就凉凉了 推是很不好推出来的 假设我们这里有两个方程: x=a1?x1+b1 x=a2?x2+b2 a1,a2是模数,b1,b2是余数 那么我们可以合并这两个方程: a1?x1+b1=a2?x2+b2 由于x1和x2可以取负无穷到正无穷,所以符号不能约束它们,我们随便变一变形得到 a1?x1+a2?x2=b2?b1 然后使用拓展欧几里德算法,x和y分别是式子中的x1和x2 我们求出了一个最小正整数解x1 令k=(a1

解模线性方程组 非互质中国剩余定理

首先咱们得感谢KIDx大神给出这样的解法. 这里是我所学习这个算法的地方:http://972169909-qq-com.iteye.com/blog/1266328 . 我将对这个算法进行一定的总结与梳理,以及小地方的修正. 今有物不知其数,三三数之余二:五五数之余三:七七数之余二.问物几何? 这是经典的孙子定理.我们注意到其中的模数都是互质的,这样可以让我们进行传统孙子定理中的转化与合并. 但是如果遇到不是互质的模线性方程组我们要怎么办呢? [主要使用手段:合并方程] 利用一定手段,不断的合

转载----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

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: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,