链接:

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. [转帖]UCloud上市:利润暴跌84%、成本居高不下,结构化调整迫在眉睫

    UCloud上市:利润暴跌84%.成本居高不下,结构化调整迫在眉睫 https://www.iyiou.com/p/116317.html     [ 亿欧导读 ] 日前,上交所科创板上市委召开第27 ...

  2. hexo 个人博客搭建

    Hexo 小插曲介绍 虽然标题是第一次写博客. 但是我这个困难户至少挣扎了1年多了, 一直下不去手.今天可算是开了个头. 贵在坚持吧 抽时间介绍我的hexo安装历程吧,今天实在是有点困了,要睡觉了. ...

  3. Lombok - 使用注解让你的JavaBean变得更加简洁

    Lombok - 工具简介: Lombok是一个编译时注释预处理器,有助于在编译时注入一些代码.Lombok提供了一组在开发时处理的注释,以将代码注入到Java应用程序中,注入的代码在开发环境中立即可 ...

  4. AVR单片机教程——EasyElectronics Library v1.0手册

    更新:EasyElectronics Library v1.1手册 索引: bit.h delay.h pin.h tone.h pwm.h uart.h adc.h led.h rgbw.h seg ...

  5. mongodb 启动及创建用户

    1. 守护进程启动,参考: https://blog.csdn.net/jj546630576/article/details/81117765 2. 用户管理参考: https://www.cnbl ...

  6. 轻松玩转Ant Design Pro一

    ant design pro来源于ant design,其是一段自带样式的react组件,用于企业后台的漂亮的,可控的组件.ant design有很多组件和样式,不可能所有都记住,我们只要记住常用的, ...

  7. Android SDK版本号 与 API Level 对应关系 201911

    API是开发用的,所以API LEVEL可以认为是内部的:而SDK的版本提供了新特性给用户,是外部可见的. 可以查看以下网址以获取最新的对应关系:  http://developer.android. ...

  8. python 循环结构(for-in)

    循环结构(for-in) 说明:也是循环结构的一种,经常用于遍历字符串.列表,元组,字典等 格式: for x in y: 循环体 执行流程:x依次表示y中的一个元素,遍历完所有元素循环结束 示例1: ...

  9. iOS 内存管理的一点小问题

    现在大家的项目应该基本都是ARC了,如果还是MRC的话,赶紧转换到ARC吧!最近被临时拉过去开发iPad,由于项目原因,还是使用的MRC.今天在调部分界面的时候,发现一段代码,我怎么看都怎么觉得怪怪的 ...

  10. ESLint——从零学起

    介绍 ESLint最初是由Nicholas C. Zakas于2013年6月创建的开源项目.它的目标是提供一个插件化的javascript代码检测工具.因此,ESLint就是一个语法规则和代码风格的检 ...