链接:

https://vjudge.net/problem/POJ-2891

题意:

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

思路:

考虑同余方程组:

\(x \equiv a_1(mod m_1)\)

\(x \equiv a_2(mod m_2)\)

...

当求第i个式子时,我们有前i-i个方程的特解\(x\),通解\(x+i*m\),\(m\)为前i-1个方程\(m\)的lcm。

考虑第i个式子,\(x+t*m \equiv a_i (mod m_i)\),解除最小的t即可。

上式可转为\(t*m + (-k)*m_i = a_i-x\),用扩展欧几里得即可得到最小解。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h> using namespace std;
typedef long long LL;
const int INF = 1e9; const int MAXN = 1e5+10;
LL A[MAXN], M[MAXN];
int n; LL ExGcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL d = ExGcd(b, a%b, x, y);
LL tmp = x;
x = y;
y = tmp-(a/b)*y;
return d;
} LL ExCRT()
{
LL res = A[1], m = M[1];
for (int i = 2;i <= n;i++)
{
LL d, x, y;
d = ExGcd(m,M[i], x, y);
if ((A[i]-res)%d)
return -1;
x = x*(A[i]-res)/d;
//cout << x << ' ' << y << ' ' << d << endl;
x = (x%(M[i]/d)+(M[i]/d))%(M[i]/d);
res = res+x*m;
m = (m*M[i])/d;
res %= m;
}
return (res%m+m)%m;
} int main()
{
while(~scanf("%d", &n))
{
for (int i = 1;i <= n;i++)
scanf("%lld%lld", &M[i], &A[i]);
printf("%lld\n", ExCRT());
} return 0;
}

POJ-2891-Strange Way to Express Integers(线性同余方程组)的更多相关文章

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

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

  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: 10907 ...

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

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

  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. POJ2891Strange Way to Express Integers (线性同余方程组)

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

  7. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  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 ...

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

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

随机推荐

  1. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  2. Tomcat部分知识点小结

    * Tomcat:web服务器软件     1. 下载:http://tomcat.apache.org/    2. 安装:解压压缩包即可. * 注意:安装目录建议不要有中文和空格    3. 卸载 ...

  3. WUSTOJ 1302: 区间k大数查询(Java)

    题目链接:

  4. Linux下实现web服务器

    说明:暂时只是实现了静态网页的响应 #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include < ...

  5. OpenCV学习笔记3

    OpenCV学习笔记3 图像平滑(低通滤波) 使用低通滤波器可以达到图像模糊的目的.这对与去除噪音很有帮助.其实就是去除图像中的高频成分(比如:噪音,边界).所以边界也会被模糊一点.(当然,也有一些模 ...

  6. 十六、USB驱动

    一.USB固件和USB传输方式 USB固件: USB固件一般不需要我们编写,在此不做程序分析. USB固件中包含USB设备的出厂信息,如厂商ID.产品ID.主版本号和次版本号等.这就是为什么当我们把U ...

  7. Webpack将静态资源拷贝并压缩至输出文件夹

    就拿Vue项目来说,比如要将src/assets/js下的静态js文件,直接在public/index.html中引用: 这时候没有在项目中引用,不会经过wenpack的loader,也就不会自己打包 ...

  8. 自定义AuthorizeFilter

    using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization.Infrastructure; u ...

  9. windows下批处理保留指定日期下的文件

    @echo offchcp 65001setlocal enabledelayedexpansion ::设置操作路径set "pic_dir=D:\465"echo 开始清理.. ...

  10. linux文件比较

    Linux文件比较指令有两个,comm和diff,其中comm要求的是排序过得文件.Diff则没有这个要求,diff的输出结果主要是用来表明文件一经过怎样的修改可以得到文件二. Comm Comm的语 ...