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 ...
随机推荐
- Struts2基础入门
Struts2基础入门 创建一个web工程 0)导包并且创建一个核心配置文件 <?xml version="1.0" encoding="UTF-8"?& ...
- IBM Rational Software Architect V9.0安装图解
IBM Rational Software Architect(RSA) -- IBM软件开发平台的一部分 – 是IBM在2003年二月并购Rational以来,首次发布的Rational产品.改进过 ...
- php性能优化 --- laravel 性能优化
1.laravel官方提供了一些优化(laravel 5.* 版本): (1).关闭debug,修改 .env 的 APP_DEBUG=false (2). sudo php artisan ...
- day1_作业2(三级菜单)
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- province={'江苏省':['南京市','苏州市','无锡市'],'浙江省':['杭州市','温州 ...
- 【转载】java 客户端链接不上redis解决方案 (jedis)
本文出自:http://blog.csdn.net/lulidaitian/article/details/51946169 出现问题描述: 1.Could not get a resource fr ...
- 使用python写一个最基本的mapreduce程序
一个mapreduce程序大致分成三个部分,第一部分是mapper文件,第二个就是reducer文件,第三部分就是使用hadoop command 执行程序. 在这个过程中,困惑我最久的一个问题就是在 ...
- Android 创建 SO 文件
创建工程,新建一个类,该类需要有一个static初始化块中调用System.loadLibrary("${soName}"),还需要有用native修饰的方法声明(无需实现),一个 ...
- Android面试收集录2 Broadcast Receiver详解
1.Broadcast Receiver广播接收器简单介绍 1.1.定义 Broadcast Receiver(广播接收器),属于Android四大组件之一 在Android开发中,Broadcast ...
- PHP.TP框架下商品项目的优化3-php封装下拉框函数
php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...
- J.U.C 系列 Tools之Executors
上个章节说了Tools中的其他四个工具类,本节我们来看一看工具类中的老大Executors,为什么说它是老大,肯定是因为他的功能最多最强大. 一 Executors是什么 Executors 是一个线 ...