**题意:**给你余数和除数求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. 阿里校招内推C++岗位编程题第一题 空格最少的字符串

    给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...

  2. Python决定一个变量时局部的,还是全局的,是在编译期

    Python中的变量名是在编译时就解析好的,换句话说,在编译时(也就是在交互控制台输入代码是或者import文件时),Python就已经决定一个变量应该是局部变量,还是全局变量.来看下面的例子: &g ...

  3. MFC常用数据类型

    下面这些是和Win32程序共同使用的数据类型BOOL:布尔值,取值为TRUE or FALSEBSTR:32-bit 字符指针BYTE:8-bit整数,未带正负号COLORREF:32-bit数值,代 ...

  4. iOS- 简单说说iOS移动客户端SQLite3的基本使用

    1.为什么要使用SQLite3? •大量数据需要存储 •管理数据,存储数据   SQLite是一种关系型数据库(也是目前移动客户端的主流数据库)     2.SQLite3的几种存储类型   a.NU ...

  5. 敏捷冲刺DAY3

    一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 登录界面的进一步完善 服务器搭建 建立数据库 下一步任务的规划,展望 4. 工作中遇到的困难 工作中的困难:在进行模糊查询时,由于中 ...

  6. Python使用ElementTree美化XML格式

    Python中使用ElementTree可以很方便的处理XML,但是产生的XML文件内容会合并在一行,难以看清楚. 如下格式: <root><aa>aatext<cc&g ...

  7. cacti监控多个mysql端口

    1. 在Console -> Data Templates 找到mysql-cacti-templates的mysql模板,编辑: 在这里面把Port的Use Per-Data Source V ...

  8. P1297 [国家集训队]单选错位

    题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上共有n道单选题,第i道单选题有ai个 ...

  9. Ubuntu安装teamviewer注意事项。

    Ubuntu安装teamviewer注意事项. 首先通过浏览器到官方下载ubuntu对应teamviewer的安装包 但是通过dpkg –i安装之后发现安装过程出问题,安装好的包打开之后也闪退. 这个 ...

  10. Android Intent Action 一览表

    String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT" String A ...