POJ-2891-Strange Way to Express Integers(线性同余方程组)
链接:
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(线性同余方程组)的更多相关文章
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- POJ 2891 Strange Way to Express Integers (解一元线性方程组)
求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1) 1x=r2(mod a2) ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- POJ2891Strange Way to Express Integers (线性同余方程组)
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
随机推荐
- eclipse设置text file encoding UTF-8和文件的换行符 Unix 格式
阿里华山版java开发手册代码格式第10条: 步骤:1.Window - Preferences, 2.左边选择 General - Workspace , 3.右边Text file encodin ...
- C++_向函数传递对象
向函数传递对象 1. 使用对象作为函数参数 对象可以作为参数传递给函数,其方法与传递其他类型的数据相同. 在向函数传递对象时,是通过传值调用传递给函数的. 因此,函数中对对象的任何修改均不影响调用该函 ...
- AVR单片机教程——序言
我一直觉得现在的网络环境对电子技术的学习有一点问题,但始终无法确切地指出,更何况网络上相关资源已经那么丰富. 但我觉得是问题的,无论它到底是不是问题,对我来说总归是一个问题.我学习也不算深入,很多东西 ...
- 十九、eMMC驱动框架分析
一.MMC简介 eMMC在封装中集成了一个控制器,提供标准接口并管理Nand Flash,使得手机厂商就能专注于产品开发的其它部分,并缩短向市场推出产品的时间. 对于我们来说,eMMC就是在Nand ...
- GRIT VIEW删除事件
1.点选表格后找到事件 RowCommand 2.輸入gvGroupUser_RowCommand后双击 ------注分 ...
- javascript -- 把按钮变成读秒倒计时
$('#btn').click(function(){ //设置按钮倒计时 $(this).addClass('disabled'); //把按钮变灰 $(this).attr('disabled', ...
- 在Windows上安装 Consul
使用Chocolatey(Windows包管理工具)安装 官方安装说明 https://chocolatey.org/install 安装Consul 官方安装说明 https://chocolate ...
- 【转载】C#指定文件夹下面的所有内容复制到目标文件夹下面
在涉及到文件夹操作的过程中,有时候需要将文件夹下的所有内容复制拷贝到另一个文件夹,在C#的开发中有时候会遇到这个功能需求将指定文件夹下所有的内容复制到另一个文件夹,这个过程需要遍历所有的文件和目录.此 ...
- hibernate saveorupdate方法只有更新有效果,保存没有效果
转自:https://blog.csdn.net/KAIXINLUOYE/article/details/72821014 单主键生成策略由native改成assigned后,问题解决.
- windows下oracle数据库报错ORA-12705解决方法
转自:http://blog.sina.com.cn/s/blog_16eaf6b940102x66q.html 有个朋友,他们那边windows虚拟机重启后,数据库不能起来报错ORA-12705无法 ...