poj2891--Strange Way to Express Integers(不互素的中国剩余定理)

题目链接:点击打开链接

题目大意:有一个数x,x%ai = ri ,给出n对ai和ri,问x的最小非负整数是什么,如果不存在输出-1

不互素的中国剩余定理:

x%a1= r1 ; x%a2 = r2 ; 设k1,k2得到x = a1*k1 + r1 , x = a2*k2+r2

那么a1*k1+r1 = a2*k2+r2 --> a1*k1 = (r2-r1) + a2*k2---->对整个式子进行a2取余,得到(a1*k1)%a2 = (r2-r1)%a2,这里面只有一个未知量k1,用扩展gcd求出k1,计算出x = a1*k1+r1

这个x只是满足x%a1= r1 ; x%a2 = r2 ;不一定满足x%a3 = r3,所以求出的x只是真正的解的一个特解,真正的解围ans

那么ans ≡ x%lca(a1,a2)  --> 其中只有ans是一个未知数,所以就将两个式子转换为了一个式子,这样不断的合并,求最终的结果。

判断是不是有解,在扩展gcd中求k1时判断。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
void gcd(LL a,LL b,LL &d,LL &x,LL &y) {
    if(b == 0) {
        d = a ;
        x = 1 ;
        y = 0 ;
    }
    else {
        gcd(b,a%b,d,y,x);
        y = (a/b)*x ;
    }
    return ;
}
int main()
{
    LL k , a1 , a2 , r1 , r2 , d , x , y ;
    while(scanf("%I64d", &k)!=EOF)
    {
        LL flag = 1 ;
        scanf("%I64d %I64d", &a1, &r1);
        k-- ;
        while(k--)
        {
            scanf("%I64d %I64d", &a2, &r2);
            gcd(a1,a2,d,x,y);
            if( (r2-r1)%d )
                flag = 0 ;
            if( flag )
            {
                x = (r2-r1)/d*x ;
                y = a2/d ;
                x = ( x%y +y)%y ;
                r1 = x*a1 + r1 ;
                a1 = (a1*a2)/d ;
            }
        }
        gcd(1,a1,d,x,y);
        if( r1%d )
            flag = 0 ;
        if(flag == 0)
            printf("-1\n");
        else
        {
            x = r1/d*x ;
            y = a1 / d ;
            x = ( x%y+y )%y ;
            printf("%I64d\n", x);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 03:53:14

poj2891--Strange Way to Express Integers(不互素的中国剩余定理)的相关文章

解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 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 a1, a2, 

POJ2891——Strange Way to Express Integers(模线性方程组)

Strange Way to Express Integers DescriptionElina 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 n

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

POJ2891 Strange Way to Express Integers【一元线性同余方程组】

题目链接: http://poj.org/problem?id=2891 题目大意: 选择k个不同的正整数a1.a2.-.ak,对于某个整数m分别对ai求余对应整数ri,如果 适当选择a1.a2.-.ak,那么整数m可由整数对组合(ai,ri)唯一确定. 若已知a1.a2.-.ak以及m,很容易确定所有的整数对(ai,ri),但是题目是已知a1. a2.-.ak以及所有的整数对(ai,ri),求出对应的非负整数m的值. 思路: 题目可以转换为给定一系列的一元线性方程 x ≡ r1( mod a1

poj2891 Strange Way to Express Integers

扩展欧几里得,注意防溢出. http://poj.org/problem?id=2891 1 #include <cstdio> 2 using namespace std; 3 typedef __int64 LL; 4 const int maxn = 1e5 + 10; 5 6 LL a[maxn], r[maxn]; 7 int n; 8 LL egcd(LL a, LL b, LL& x, LL& y){ 9 if(!b){ 10 x = 1, y = 0; 11 r

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

【POJ2891】Strange Way to Express Integers(拓展CRT)

[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream> #include<cstdio> using namespace std; #define ll long long #define MAX 111111 ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1,y=0;return a;

【EXCRT模板】POJ2891/LuoGu4777Strange Way to Express Integers拓展中国剩余定理

这道题需要exgcd的基础 POJ的题干描述十分恶心 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21217 Accepted: 7120 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negati

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

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