P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers
excrt模板
我们知道,crt无法处理模数不两两互质的情况
然鹅excrt可以
设当前解到第 i 个方程
设$M=\prod_{j=1}^{i-1}b[j]$ ,$ res$是前$ i-1 $个方程的最小解
则$ res+x*M$ 是前 $i-1 $个方程的通解
那么我们求的就是
$res+x*M ≡ a[i] (mod b[i])$
$<=> x*M - y*b[i] = a[i]-res$
用exgcd求出的解为 t (当且仅当 gcd(M,b[i])∣t 时有解)
x的一个解=$ t /gcd(M,b[i])*(a[i]-res)$
最小解=$ x\%( b[i] / gcd(M,b[i]) )$
∴$res=(res+x*M)\%( M=M*b[i] )$
如此递推
end.
poj2891 有多组数据,请自行修改(poj只能用 I64d 来着(大雾))
#include<cstdio>
#include<cctype>
using namespace std;
typedef long long ll;
char c;template<typename T>void read(T &x){
c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
}
ll mod(ll x,ll p) {return x< ?x+p:x;}
ll mul(ll x,ll y,ll p){
ll tmp=x*y-(ll)((long double)x/p*y+1.0e-8)*p;
return mod(tmp,p);
}ll g,a[],b[];
void exgcd(ll a0,ll b0,ll &x,ll &y){
if(!b0) x=,y=,g=a0;
else exgcd(b0,a0%b0,y,x),y-=x*(a0/b0);
}int n;
ll excrt(){
ll res=a[],M=b[],x,y,c,t,B;
for(int i=;i<=n;++i){
B=b[i];
c=mod((a[i]-res)%B,B);
exgcd(M,B,x,y); t=B/g;
if(c%g) return -;
x=mul(x,c/g,t=B/g); 快速乘取模
res+=x*M; M*=t;
res=mod(res%M,M);
}return res;
}
int main(){
read(n);
for(int i=;i<=n;++i) read(b[i]),read(a[i]);
printf("%lld",excrt());
return ;
}
P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers的更多相关文章
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- 扩展中国剩余定理 (exCRT) 的证明与练习
原文链接https://www.cnblogs.com/zhouzhendong/p/exCRT.html 扩展中国剩余定理 (exCRT) 的证明与练习 问题模型 给定同余方程组 $$\begin{ ...
- 中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结
中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300035 前置浅讲 前 ...
- 扩展中国剩余定理 (ExCRT)
扩展中国剩余定理 (ExCRT) 学习笔记 预姿势: 扩展中国剩余定理和中国剩余定理半毛钱关系都没有 问题: 求解线性同余方程组: \[ f(n)=\begin{cases} x\equiv a_1\ ...
- [poj2891]Strange Way to Express Integers(扩展中国剩余定理)
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- POJ2891 Strange Way to Express Integers【扩展中国剩余定理】
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...
- POJ2891 Strange Way to Express Integers (扩展欧几里德)
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r ...
随机推荐
- Linux 文件系统剖析
[转自]https://www.ibm.com/developerworks/cn/linux/l-linux-filesystem/ 按照分层结构讨论 Linux 文件系统 在文件系统方面,Linu ...
- pyqt4手动编写资源文件
资源文件resource.qrc为XML格式,格式较简单,可以手动编辑: <!DOCTYPE RCC><RCC version="1.0"> <qre ...
- a排兵布阵
来源hdu1166 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵 ...
- E - Stones 优先队列
来源1896 Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning an ...
- A - Black Box 优先队列
来源poj1442 Our Black Box represents a primitive database. It can save an integer array and has a spec ...
- 使用soap遇到的缓存问题
- 28、gulp
一.gulp的作用? 1.gulp-sass : 编译sass 2.gulp-rename : 重命名 3.gulp-cssnano : 压缩css 4.gulp-concat : 合并文件 5.gu ...
- git push fatal: The remote end hung up unexpectedly
git push fatal: The remote end hung up unexpectedly git config http.postBuffer git gc --aggressive 不 ...
- 计算属性和监听,computed,watch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js隐藏中间4位,变成‘*’号
var tel = "15222622548"; var reg = /^(\d{3})\d{4}(\d{4})$/; tel = tel.replace(reg, "$ ...