题面就是让你解同余方程组(模数不互质)


题解:

先考虑一下两个方程

x=r1 mod(m1)

x=r2 mod (m2)

去掉mod

x=r1+m1y1   ......1

x=r2+m2y2   ......2

1-2可以得到

m1y1-m2y2=r1-r2

形同ax+by=c形式,可以判无解或者解出一个y1的值

带回1式可得到一个x的解x0=r1-y1a1

通解为x=x0+k*lcm(m1,m2)

即x=x0 mod(lcm(m1,m2))

令M=lcm(m1,m2) R=x0

所以x满足x=R mod(M)

就变成了一个新的式子

可以合并到最后啦

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100010
typedef long long ll;
using namespace std;
ll n,m[N],r[N];
ll exGcd(ll a,ll b,ll &x,ll &y)
{
if (b==) return x=,y=,a;
ll r=exGcd(b,a%b,y,x);
y-=a/b*x;
return r;
}
ll solve()
{
ll M=m[],R=r[],x,y,d;
for (int i=;i<=n;i++)
{
d=exGcd(M,m[i],x,y);
if ((R-r[i])%d!=) return -;
x=(R-r[i])/d*x%m[i];
R-=x*M;
M=M/d*m[i];
R%=M;
}
return (R%M+M)%M;
}
int main()
{
while (scanf("%lld",&n)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%lld%lld",&m[i],&r[i]);
printf("%lld\n",solve());
}
return ;
}

POJ 2891 Strange Way to Express Integers | exGcd解同余方程组的更多相关文章

  1. POJ 2891 Strange Way to Express Integers (解一元线性方程组)

    求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1)     1x=r2(mod a2)     ...

  2. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  3. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  7. poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组

    怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equ ...

  8. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  9. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

随机推荐

  1. FAT32中文版分析+补充(2)

    从Offset 36(0x24)开始FAT12/16的内容开始区别于FAT32,现在分两个表格列出来,下表为FAT12/16的内容: 名称 Offset(Byte) 大小(Byte) 描述 BS_dr ...

  2. 基于mybatis设计简单信息管理系统2

    1.空指针异常 public class CanvasServlet extends HttpServlet { private CanvasService canvasService; privat ...

  3. MySQL超大表如何提高count速度

    经常用到count统计记录数,表又超级大,这时候sql执行很慢,就是走索引,也是很慢的,怎么办呢? 1.这个时候我们就要想为什么这么慢:根本原因是访问的数据量太大,就算只计算记录数也是很慢的. 2.如 ...

  4. C#基础-面向对象-封装

    封装 命名空间 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp6 { c ...

  5. give me something new 无用但有趣

    屏保系列 http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gz  //数码雨 libaa-bin //燃烧 海洋馆 http://search.cp ...

  6. 如何在CentOS 7.1中安装VMware Workstation

    VMware Workstation在windows环境中大家都会安装,最近公司有个需求是需要在CentOS环境中安装VMware Workstation,上官网查了下还真有Linux版本的,于是就开 ...

  7. C# 不同窗口传递参数

    form1: private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm. ...

  8. python——标准异常总结

    请参考此网站: Python 标准异常总结 https://fishc.com.cn/forum.php?mod=viewthread&tid=45814&extra=page%3D1 ...

  9. [Codeforces976E]Well played!(贪心)

    [不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...

  10. HDFS HA 的 core-site.xml

    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Licens ...