中国剩余定理模数不互质的情况(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 ...
随机推荐
- 其它课程中的python---1、python基础
其它课程中的python---1.python基础 一.总结 一句话总结: 可以先把视频平台搭起来,这样学习效率会高很多,而且有额外收益 1.python的优势有哪些? 一个广泛的标准库 扩展性:比如 ...
- C++之引用与符号“&”
一.&的意思: 1.取地址符,这时候它用于数据的前面,比如int a=&b; 2.C++里还使用&作为引用符,如果你确认程序是标准的C而非C++的话,那么可以排除是引用了.引用 ...
- How to Add Swap on CentOS
About Linux Swapping Linux RAM is composed of chunks of memory called pages. To free up pages of RAM ...
- idea 设置默认maven auto import,不需要每次都弹出
1 2 误区 从这里点进去进行设置,或者每次打开项目右击下方的 auto impot都是不对的.
- Day 11:函数装饰器
在说装饰器前,先说一个东西,再Python里,有一个 一切皆对象,一切皆变量. 例: def hello(name="sunjinyao"): return "hi &q ...
- python简介与安装
Python简介和环境搭建 于 20世纪80年代末,Guido van Rossum发明了Python,初衷据说是为了打发圣诞节的无趣.1991年首次发布,是ABC语言的继承,同时也是一种脚本语言.取 ...
- D3.js坐标轴的绘制方法、添加坐标轴的刻度和各比例尺的坐标轴(V3版本)
坐标轴(Axis) 坐标轴(Axis)在很多图表中都可见到,例如柱形图.折线图.散点图等.坐标轴由一组线段和文字组成,坐标轴上的点由一个坐标值确定.但是,如果使用SVG的直线和文字一笔一画的绘制坐 ...
- 转: PHP中this,self,parent的区别
{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...
- JPA Query in 集合
使用 :param的方式来传递参数,下面举个例子 @PersistenceContext EntityManager em @Override public List<Map> ...
- heartbeat 高可用
转载来自 http://www.cnblogs.com/liwei0526vip/p/6391833.html 使用HeartBeat实现高可用HA的配置过程详解 一.写在前面 HA即(high av ...