中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况
主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小
https://vjudge.net/problem/POJ-2891

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2019.8.8
using namespace std;
long long T,n;
long long r[N],a[N],x,y,gcd,flag;
void in(long long &x){
long long y=;char c=getchar();x=;
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c<=''&&c>=''){ x=(x<<)+(x<<)+c-'';c=getchar();}
x*=y;
}
void o(long long x){
if(x<){p('-');x=-x;}
if(x>)o(x/);
p(x%+'');
} void exgcd(long long a,long long b,long long &x,long long &y){
if(!b){
x=;y=;gcd=a;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
} signed main(){
while(scanf("%lld",&n)!=EOF){
flag=;
For(i,,n)
in(r[i]),in(a[i]);
For(i,,n){
exgcd(r[],r[i],x,y);
if((a[i]-a[])%gcd==){
x*=(a[i]-a[])/gcd;
y=r[i]/gcd;
x=(x%y+y)%y;
a[]+=r[]*x;
r[]=r[]*r[i]/gcd;
}
else{
o(-);p('\n');
flag=;
break;
}
}
if(!flag)
o(a[]%r[1]),p('\n');
}
return ;
}
中国剩余定理模数不互质的情况(poj 2891的更多相关文章
- POJ 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- poj 2891 模数不互质的中国剩余定理
Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which intr ...
- HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...
- 中国剩余定理模数互质的情况模板(poj1006
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- P4777 【模板】扩展中国剩余定理(EXCRT)
思路 中国剩余定理解决的是这样的问题 求x满足 \[ \begin{matrix}x \equiv a_1(mod\ m_1)\\x\equiv a_2(mod\ m_2)\\ \dots\\x\eq ...
- LUOGU P4777 【模板】扩展中国剩余定理(EXCRT)
传送门 解题思路 扩展 $crt$,就是中国剩余定理在模数不互质的情况下,首先对于方程 $\begin{cases} x\equiv a_1\mod m_1\\x\equiv a_2\m ...
随机推荐
- Java-Class-@I:org.springframework.beans.factory.annotation.Autowired
ylbtech-Java-Class-@I:org.springframework.beans.factory.annotation.Autowired 1.返回顶部 2.返回顶部 1. pack ...
- Ubuntu 18.04.2 aliases 设置永久生效解决方案
设置 临时 alias alias ll="ls -al" 缺点是下次登录时就不生效了 永久生效解决方案 进入到 etc 文件夹下 cd /etc/ 创建 bash_aliases ...
- 测试VPS
wget freevps.us/downloads/bench.sh -O - -o /dev/null|bash
- Docker搭建Portainer可视化界面
为了解决上回说到的问题,在网上找了找 找到了一个 非常有好的可视化界面管理工具. Portainer 是什么东西 (开源轻量级) Portainer是Docker的图形化管理工具,提供状态显示面板.应 ...
- 17-Ubuntu-文件和目录命令-切换目录-相对路径和绝对路径
1.相对路径: 在输入路径时,最前面不是/或者~,表示相对当前目录所在的目录位置. 例:当前桌面目录下,通过相对路径切换到桌面目录下的Entertainment目录 2.绝对路径: 在输入路径时,最前 ...
- Redis连不上的一些细节配置
远程链接redis连不上,在确保防火墙设置正确的情况下 把redis.conf中的 bind 127.0.0.1注释 另外把protected-mode yes 改为protected-mode no
- mysql 03章_完整性、约束
.完整性:数据库中数据的可靠性有效性和合理性我们称为数据的完整性,这样才能保证数据合理符合现实生活中的数据体现. 注:数据完整性的设计应该在设计表的时候就进行设计了,而不是等到数据库中已经存在数据才进 ...
- vba增删改查数据库2
sub test()Set cnn = CreateObject("ADODB.Connection") Set rs = CreateObject("Adodb.Rec ...
- 使用Echarts的步骤
Echarts官网地址:https://echarts.baidu.com/index.html 步骤如下: 1.获取Echarts (1)可以在Echarts官网去下载,选择需要的版本下载,根据开发 ...
- Buy Low, Buy Lower
Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...