中国剩余定理模数不互质的情况
主要有一个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的更多相关文章

  1. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...

  2. Hello Kiki(中国剩余定理——不互质的情况)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  3. poj 2891 模数不互质的中国剩余定理

    Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which intr ...

  4. HDU3579Hello Kiki(中国剩余定理)(不互质的情况)

    One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...

  5. 中国剩余定理模数互质的情况模板(poj1006

    http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...

  6. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

  7. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

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

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

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

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

随机推荐

  1. ionic-CSS:ionic select

    ylbtech-ionic-CSS:ionic select 1.返回顶部 1. ionic select ionic select 的 select 相比原生的要更加美观些.但是弹出的可选选项样式是 ...

  2. LED 发光二极管压降

    常用发光二极管的压降 1. 直插超亮发光二极管压降 主要有三种颜色,然而三种发光二极管的压降都不相同,具体压降参考值如下: 红色发光二极管的压降为2.0--2.2V  黄色发光二极管的压降为1.8—2 ...

  3. 如何把本地文件上传github

    1.$ git config --global user.name "xxx" 2.$ git config --global user.email xxx@qq.com 3.进入 ...

  4. mybatis浅显认识

    mybatis主配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configu ...

  5. Merge k Sorted Arrays【合并k个有序数组】【优先队列】

    Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...

  6. 【POJ】2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: #include<iostream> #include& ...

  7. mysql中重复数据只取条

    select * from table_a where id in (select min(id) from table_a group by a) )) SUBSTRING_INDEX(cids,' ...

  8. JS对象 神奇的Math对象,提供对数据的数学计算。注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别

    Math对象 Math对象,提供对数据的数学计算. 使用 Math 的属性和方法,代码如下: <script type="text/javascript"> var m ...

  9. adb命令 查看运行APP当前页面的Activity名称

    命令 adb shell "dumpsys window | grep mCurrentFocus" 结果

  10. Java 异常基本结构

    异常的定义:阻止当前方法或作用域继续执行的情况,即程序无法正常执行下去称之为异常. 异常的基本结构: 所有不正常的类都继承与Throwable类,包括Error类和Exception类 Error一般 ...