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 ...
随机推荐
- pdo->prepare 返回false的问题总结
报错信息: Fatal error: Call to a member function execute() on a non-object 一般是pdo->prepare 返回了false导致 ...
- hdu_3501_Calculation 2
Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...
- Java反射+注解案例
注解类代码,注解的属性可以有多个: package reflect; import java.lang.annotation.Retention; import java.lang.annotatio ...
- Servlet学习笔记02——什么是http协议?
1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...
- JAVA / MySql 编程——第八章 DAO 模式
1. 数据持久化:将程序中的数据在瞬时状态和持久状态间转换的机制即为数据持久化: 2. 持久化的实现方式:数据库.普通文件.XML文件: 3. JDBC封装: ...
- CSS选取指定位置标签first-child、last-child、nth-child
1.first-child 选择列表中的第一个标签. 2.last-child 选择列表中的最后一个标签 3.nth-child(n) 选择列表中的第n个标签 4.nth-child(2n) 选择列表 ...
- CF797E. Array Queries
a is an array of n positive integers, all of which are not greater than n. You have to process q que ...
- 十三、MySQL之IDE工具介绍及数据备份
一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 二.MySQL数据备份 # ...
- python lamba表达式
lambda函数也叫匿名函数,即,函数没有具体的名称. g=lambda x:x**2 def f(x): return x**2 lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边是 ...
- spring里面的context:component-scan
原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...