**题意:**给你余数和除数求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. 蓝牙核心技术概述(四):蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM)(转载)

    一.主机控制接口协议  HCI 蓝牙主机-主机控模型 蓝牙软件协议栈堆的数据传输过程: 1.蓝牙控制器接口数据分组:指令分组.事件分组.数据分组(1).指令分组 如:Accpet Connection ...

  2. 自测之Lesson13:共享内存

    题目:创建一个64K的共享内存. 实现代码: #include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> ...

  3. 福大软工1816:Alpha(4/10)

    Alpha 冲刺 (4/10) 队名:Jarvis For Chat 组长博客链接 本次作业链接 团队部分 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.将中文分词.词频 ...

  4. erlang+thrift配合开发

    I  think, thrift is a  tcp/ip based Client-Server architecture multi-languages supported RPC framewo ...

  5. Oracle AWR日志使用

    SQL>@?/rdbms/admin/awrrpt.sql Specify the Report Type ~~~~~~~~~~~~~~~~~~~~~~~ Would you like an H ...

  6. Qt Meta Object system 学习

    原文地址:http://blog.csdn.net/ilvu999/article/details/8049908 使用 meta object system 继承自 QOject 类定义中添加 Q_ ...

  7. Java取两个变量不为空的变量的简便方法!

    一.需求 最近在项目中遇到一个小问题,即从数据库取两个变量,判断取出的变量是否为空,取不为空的变量:若两个变量都不为空,取两个变量:两个变量都为空,则跳过: 二.解决方案(这里提供两种思路) 1.第一 ...

  8. 钉钉 E应用 打开分享外链

    钉钉 E应用 打开分享外链 外部链接 https://open-doc.dingtalk.com/microapp/dev https://open-doc.dingtalk.com/microapp ...

  9. 2.StringBuffer:线程安全的可变字符串序列

    一.String.StringBuffer和StringBuilder的区别 1.String是内容不可变的,而StringBuffer和StringBuilder都是内容可变的. 2.StringB ...

  10. CentOS 文件搜索find

    1.文件搜索,内置的的命令是find 用法: find [查找路径] 寻找条件 操作 默认路径为当前目录:默认表达式为 -print 2.主要参数: -name 匹配名称 -perm 匹配权限(mod ...