**题意:**给你余数和除数求x 注意除数不一定互质
**思路:**不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质

合并互质求余方程

m1x -+ m2y = r2 - r1 先用exgcd求出特解x0, y0(m1x + m2y = g)

等式两边都乘以c/g 那么可以得解为 x1 = cx0/g,y1 = cy0/g

由性质可知 通解x' = x1 + km2/g,y' = y1 + km1/g 其中k为任意整数

即 x' = cx0/g + km2/g

设 t = m2/g

为保证x是正数 可以将式子化成 x' = (cx0/d % t + t)%t 因为必定存在k使 cx0/g + kt = ( (cx0/g)%t + t)%t

最后合并的取余式为 X = (m1 * x' + r1) (mod lcm(m1, m2) )

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define LL long long

#define ll __int64

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+2000;



ll gcd(ll a, ll b)

{

return b?gcd(b, a % b):a;

}



ll exgcd(ll a, ll b, ll &x, ll &y)

{

ll d = a;

if(!b)

{

x = 1;

y = 0;

}

else

{

d = exgcd(b, a % b, y, x);

y -= (a / b) * x;

}

return d;

}




int main()

{

ll n;

while(~scanf("%I64d", &n))

{

ll x, y;

ll m, r;

ll mt, rt;

int flag = 0;

scanf("%I64d%I64d", &m, &r);

for(int i = 0; i < n - 1; i++)

{

scanf("%I64d%I64d", &mt, &rt);

ll c = rt - r;

ll g = gcd(m, mt);

if(c % g!=0)

{

flag = 1;

}

else

{

exgcd(m, mt, x, y);

ll t = mt / g;

x = (c / g * x % t + t) % t;

r = m * x + r;

m = m /gcd(m, mt) * mt;

}

}

if(flag == 1)

printf("-1\n");

else printf("%I64d\n", r);

}

return 0;

}

POJ 2891- Strange Way to Express Integers CRT 除数非互质的更多相关文章

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

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  2. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

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

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  5. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  6. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

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

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

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

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  9. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

随机推荐

  1. 第八章 IO库

    8.1&&8.2 #include <iostream> #include <vector> #include <string> using nam ...

  2. 11.24Daily Scrum

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.990 测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.991 测试 王宇杰 负责后台代码测 ...

  3. MyBatis 基本构成与框架搭建

    核心组件 SqlSessionFactoryBuilder (构造器) 根据配置信息(eg:mybatis-config.xml)或者代码来生成SqlSessionFactory. SqlSessio ...

  4. Swift-枚举enum理解

    //定义一个枚举 //枚举的语法,enum开头,每一行成员的定义使用case关键字开头,一行可以定义多个关键字 enum CompassPoint { case North case South ca ...

  5. TCP源码—连接建立

    一.SYN报文处理: 公共部分:tcp_v4_rcv->tcp_v4_do_rcv->tcp_v4_cookie_check(无处理动作)->tcp_rcv_state_proces ...

  6. 【MVC4升级到MVC5】ASP.Net MVC 4项目升级MVC 5的方法

    1.备份你的项目 2.从Web API升级到Web API 2,修改global.asax,将 ? 1 WebApiConfig.Register(GlobalConfiguration.Config ...

  7. web前端之路 - 开篇

    一 web发展历程 了解事物的历史有助于我们渐进式的从发展的思路清楚了解事物的来龙去脉. 这里有一篇网文写得比较清晰和完整:https://www.tianmaying.com/tutorial/we ...

  8. string字符串比较和替换

    我用的是小写的string!! #include <string> #include <iostream> using namespace std; int main() { ...

  9. Lucene笔记二

    lucene 的排序 package cn.itcast.lucene; import java.io.IOException; import org.apache.lucene.document.D ...

  10. bzoj1086-王室联邦

    题目 给出一棵树,求一种分块方案,使得每个块的大小\(size\in [B,3B]\).每个块还要选一个省会,省会可以在块外,但是省会到块内任何一个点路径上的所有除了省会的点都必须属于这个块.\(n\ ...