题目大意

就是模板。。。没啥好说的

思路

因为模数不互质,所以直接中国剩余定理肯定是不对的

然后就考虑怎么合并两个同余方程

\(ans = a_1 + x_1 * m_1 = a_2 + x_2 * m_2\)

\(x_1 * m_1 + x_2 * m_2 = a _ 2 - a _ 1\)(因为正负号没影响嘛)

然后就可以exgcd解出来\(x_1, x_2\), 最后就可以得到\(x' = a_1 + x_1 * m_1, m' = lcm(m_1, m_2)\)

然后就不停合并就可以了


//Author: dream_maker
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e5 + 5;
ll n, a[N], m[N];
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {x = 1, y = 0; return;}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
ll exCRT() {
ll M = m[1], A = a[1];
fu(i, 2, n) {
ll d = gcd(M, m[i]), x, y;
if ((a[i] - A) % d) return -1;
exgcd(M, m[i], x, y);
x *= (a[i] - A) / d;
x = (x % (m[i] / d) + (m[i] / d)) % (m[i] / d);
A += M * x, M = M / d * m[i], A %= M;
}
if (A < 0) A += M;
return A;
}
int main() {
while (scanf("%lld", &n) != EOF){
fu(i, 1, n) Read(m[i]), Read(a[i]);
Write(exCRT());
putchar('\n');
}
return 0;
}

POJ2891 Strange Way to Express Integers【扩展中国剩余定理】的更多相关文章

  1. [poj2891]Strange Way to Express Integers(扩展中国剩余定理)

    题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...

  2. POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)

    题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...

  3. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  4. POJ2891 Strange Way to Express Integers (扩展欧几里德)

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r ...

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

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

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

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  7. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  8. P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers

    P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...

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

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  10. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

随机推荐

  1. 编译Python2.7.10

    为了测试 mesos,搞了一个 centos7.1,使用最小化安装,然后自己安装了 net-tools,“开发工具”集.后来想装一下 DCOS Cli工具,结果发现 python 的 pip 不可用. ...

  2. [BZOJ3244][NOI2013]树的计数

    这题大家为什么都写O(NlogN)的算法呢?…… 让本蒟蒻来写一个O(N)的吧…… 首先还是对BFS序和DFS序重编号,记标好的DFS序为d[1..n].令pos[x]为x在d[]中出现的位置,即po ...

  3. Poi中getPhysicalNumberOfCells 与 getLastCellNum的差异

    getPhysicalNumberOfCells 与 getLastCellNum的区别 用org.apache.poi的包做excel导入,无意间发明若是excel文件中有空列,空列后面的数据全部读 ...

  4. 清理网页中的HTML

    public string ClearHtml(string text)//过滤html,js,css代码 { text = text.Trim(); if (string.IsNullOrEmpty ...

  5. SpringMvc+mybatis mybatis在xml文件中大于小于号处理

    方法一:转移字符 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE = AND start_date <= CURRENT_DATE ...

  6. 小米2S手机 - Charles无法安装证书 因为无法读取证书

    1.不要使用小米原装的浏览器安装证书 2.使用第三方浏览器安装,如我使用的是UC浏览器 3.使用第三方浏览器安装的证书格式是".pem"格式问卷 4.将这个文件放入小米的downl ...

  7. JAVA执行带参数的SQL语句

    转自 http://www.cnblogs.com/raymond19840709/archive/2008/05/12/1192948.html

  8. grub2 windows版安装

    一.BIOS方式,grub2安装 查看磁盘情况 E:\grub-2.02-for-windows>wmic diskdrive list brief Caption DeviceID Model ...

  9. day27 CRM delete& action& 嵌入CRM

    课程目录:deleteactionpop up window嵌入crm项目 权限(未讲)学员交作业发邮件 代码路径:https://github.com/liyongsan/git_class/tre ...

  10. line-height:150% 和 line-height:1.5的区别

    line-height:150% 是继承父元素的距离 line-height:1.5  是计算各子元素的距离 1.当line - height 为百分比时: body{ font-size:14px; ...