EXCRT

不保证模数互质

\[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\equiv b_2\ ({\rm mod}\ a_2) \\ ... \\ x \equiv b_n\ ({\rm mod}\ a_n)\end{cases}
\]

CRT戳这里

来一手数学归纳法

设已经求出前 \(k - 1\) 组的一个解 \(q\)

设 \(M = \prod_{i = 1}^{k - 1}a_{i}\)

我们知道前 \(k - 1\) 组的通解为 \(q + xM\)

现在考虑第 \(k\) 组方程

设存在一 \(x\) 满足

\[q + xM \equiv b_{k}\ (mod\ a_{k})
\]

移一下项

\[xM \equiv b_{k} - q\ (mod\ a_{k})
\]

于是很愉快的解一下同余方程即可

如此, 我们使用扩展欧几里得算法 \(n\) 次, 便求出了方程组的解

复杂度 \(O(n \log n)\)

P4777 【模板】扩展中国剩余定理(EXCRT)

题目描述

给定 n组非负整数 a_i, b_i 求解关于 x的方程组的最小非负整数解。

Solution

注意中间运算会爆 \(longlong\) ,使用龟速乘

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 200019;
LL num, a[maxn], b[maxn];
LL x, y;
LL Q_mul(LL a, LL b, LL mod){
LL ans = 0;
while(b){
if(b & 1)ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans % mod;
}
LL exgcd(LL a, LL b, LL &x, LL &y){
if(!b){x = 1, y = 0;return a;}
LL d = exgcd(b, a % b, x, y);
LL temp = x;x = y;y = temp - (a / b) * y;
return d;
}
LL EXCRT(){
LL M = a[1], ans = b[1];
REP(i, 2, num){
LL A = M, B = a[i], C = ((b[i] - ans) % B + B) % B;
LL d = exgcd(A, B, x, y);
if(C % d != 0) return -1;
x = Q_mul(x, C / d, B / d);
ans += x * M;
M *= B / d;
ans = (ans % M + M) % M;
}
return (ans % M + M) % M;
}
int main(){
num = RD();
REP(i, 1, num)a[i] = RD(), b[i] = RD();
printf("%lld\n", EXCRT());
return 0;
}

P4777 【模板】扩展中国剩余定理(EXCRT)&& EXCRT的更多相关文章

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

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

  2. [Luogu P4777] 【模板】扩展中国剩余定理(EXCRT) (扩展中国剩余定理)

    题面 传送门:洛咕 Solution 真*扩展中国剩余定理模板题.我怎么老是在做模板题啊 但是这题与之前不同的是不得不写龟速乘了. 还有两个重点 我们在求LCM的时候,记得先/gcd再去乘另外那个数, ...

  3. 扩展中国剩余定理(EXCRT)学习笔记

    扩展中国剩余定理(EXCRT)学习笔记 用途 求解同余方程组 \(\begin{cases}x\equiv c_{1}\left( mod\ m_{1}\right) \\ x\equiv c_{2} ...

  4. 欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍

    1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x* ...

  5. P4777 【模板】扩展中国剩余定理(EXCRT)

    思路 中国剩余定理解决的是这样的问题 求x满足 \[ \begin{matrix}x \equiv a_1(mod\ m_1)\\x\equiv a_2(mod\ m_2)\\ \dots\\x\eq ...

  6. 中国剩余定理(crt)和扩展中国剩余定理(excrt)

    数论守门员二号 =.= 中国剩余定理: 1.一次同余方程组: 一次同余方程组是指形如x≡ai(mod mi) (i=1,2,…,k)的同余方程构成的组 中国剩余定理的主要用途是解一次同余方程组,其中m ...

  7. 【洛谷 P4777】 【模板】扩展中国剩余定理(EXCRT)

    注意一下:: 题目是 \[x≡b_i\pmod {a_i}\] 我总是习惯性的把a和b交换位置,调了好久没调出来,\(qwq\). 本题解是按照 \[x≡a_i\pmod {b_i}\] 讲述的,请注 ...

  8. LUOGU P4777 【模板】扩展中国剩余定理(EXCRT)

    传送门 解题思路 扩展 $crt​$,就是中国剩余定理在模数不互质的情况下,首先对于方程 ​     $\begin{cases} x\equiv a_1\mod m_1\\x\equiv a_2\m ...

  9. 【luoguP4777】【模板】扩展中国剩余定理(EXCRT)

    (扩展)中国剩余定理 对于一组同余方程 \(x\equiv a_1(mod \quad n_1)\) \(x\equiv a_2(mod \quad n_2)\) \(x\equiv a_3(mod ...

随机推荐

  1. Daily Scrum2 11.4

    昨天的任务大家都已经完成,daily scrum记录的是当日已经完成的任务. 今日任务列表: 杨伊:完成团队作业之软件测评的功能部分 徐钧鸿:CodingCook的model和helper部分 张艺: ...

  2. BufferedWriter与BufferedRead --------------------------Test

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  3. String 类 的 使用

    package com.StringUse; import java.util.Arrays; /* String 的构造方法: String() 创建一个空内容 的字符串对象. String(byt ...

  4. bata4

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组 ...

  5. Beta Scrum Day 5 — 听说

    听说

  6. AttributeError: module ‘tensorflow.python.ops.nn’ has no attribute ‘leaky_relu’

    #AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu' 的原因主要是版本的问题 解决方法是更新 ...

  7. 【动态规划】POJ-2229

    一.题目 Description Farmer John commanded his cows to search for different sets of numbers that sum to ...

  8. python learning OOP2.py

    class Student(object): pass s = Student() s.name = 'Chang' # 给一个实例动态绑定一个属性 print(s.name) def set_age ...

  9. JAVA 构造函数 静态变量

    class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println(&quo ...

  10. Prism6下的MEF:添加Logger

    上篇已经简单的构建了一个Prism的程序,现在我们需要添加一个Logger,Prism本身自带一个功能简单的TextLogger,但是我们希望能用.Net常用的Log4net.所以我们需要重载掉Boo ...