POJ 2891 Strange Way to Express Integers | exGcd解同余方程组
题面就是让你解同余方程组(模数不互质)
题解:
先考虑一下两个方程
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解同余方程组的更多相关文章
- 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
		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: 9472 ... 
- [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(拓展欧几里得)
		Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ... 
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
		题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ... 
- 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 ... 
- 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 ... 
随机推荐
- 【例题收藏】◇例题·IV◇ Wooden Sticks
			◇例题·IV◇ Wooden Sticks 借鉴了一下 Candy? 大佬的思路 +传送门+ (=^-ω-^=) 来源:+POJ 1065+ ◆ 题目大意 有n个木棍以及一台处理木棍的机器.第i个木棍 ... 
- What is EJB
			What is EJB 0.什么是EJB? 答:EJB是用于构建企业应用程序模块托管的.服务器端组件架构.EJB技术加速并简化了开发基于Java技术的分布式.事务性.安全和便携的应用程序. 先看一下E ... 
- selenium学习总结
			selenium主要用来做web自动化,分1.0和2.0两个版本,1.0包括selenium IDE.selenium Grid.selenium Remote Control,2.0在1.0的基础上 ... 
- Golang反射机制
			Go反射机制:在编译不知道类型的情况下,可更新变量.在运行时查看值.调用方法以及直接对它们的布局进行操作. 为什么使用反射 有时需要封装统一接口对不同类型数据做处理,而这些类型可能无法共享同一个接口, ... 
- linux 安装 zookeeper
			第一步 下载 zookeeper: http://archive.apache.org/dist/zookeeper/ 第二步 解压: tar -xzvf zookeeper-3.4.5.tar.gz ... 
- Ansible学习  ad-hoc命令
			Ansible提供两种方式去执行命令,一种是ad-hoc命令,一种是写入Ansible playbook.类似于前者在命令行敲shell,后者是写shell-script脚本,前者解决一些简单的任务, ... 
- <Docker学习>4. docker容器的使用
			简单的说, 容器是独立运行的一个或一组应用, 以及它们的运行态环境. 对应的, 虚拟机可以理解为模拟运行的一整套操作系统( 提供了运行态环境和其他系统环境) 和跑在上面的应用.容器的运行是基于镜像的. ... 
- 顺序链表的C风格实现
			//头文件 #ifndef _SEQLIST_H_ #define _SEQLIST_H_ //定义数据类型 typedef void SeqList; typedef void SeqListNod ... 
- 2,Flask 中的 Render Redirect HttpResponse
			一,Flask中的HTTPResponse 在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串 二,.Flask中的Redirect 每当访问"/redi" ... 
- HTTP的缓存控制
			1.缓存的分类: (1)缓存分为服务端侧(server side,比如 Nginx.Apache)和客户端侧(client side,比如 web browser). (2)服务端缓存又分为 代理服务 ... 
