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 ...
随机推荐
- BFS练习-POJ.2386
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35122 Accepted: 17437 Descr ...
- 利用Selenium+java实现淘宝自动结算购物车商品(附源代码)
转载请声明原文地址! 本次的主题是利用selenium+java实现结算购买购物车中的商品. 话不多说,本次首先要注意的是谷歌浏览器的版本,浏览器使用的驱动版本,selenium的jar包版本. ...
- mac同时安装jdk7和jdk8
下载地址:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.h ...
- linux文件属性更改命令
chown 当我们要改变一个文件的属主,我们所使用的用户必须是该文件的属主而且同时是目标属组成员,或超级用户.只有超级用户的才能改变文件的属主. chown语法: chown [选项]...[所有者 ...
- 关于Mysql唯一索引的操作方法(添加删除)
首先我们查看一下News数据表的索引信息 使用命令 show index from ‘数据表名称’; 目前数据表中仅有一个主键索引 继续,我们给news表添加两个唯一索引(两种方法) 方法一 ...
- laravels -- Swoole加速php
LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能.更多可能性. 环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高 ...
- 牛客网暑期ACM多校训练营(第四场) F Beautiful Garden
链接: https://www.nowcoder.com/acm/contest/142/F 题意: n x m的矩形,选个p x q的矩形去掉,两个矩形中⼼重合,去掉后的矩形上下左右对称 求(p, ...
- Gold Balanced Lineup POJ - 3274
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- impala presto SparkSql性能测试对比
目标是为测试impala presto SparkSql谁的性能更佳,以下结果底层查询的都是普通textfile snappy压缩后数据,规模为15台机器,若以orcfile.parquet速度能 ...
- 十四、pymysql模块
一.安装的两种方法 第一种 #安装 pip3 install pymysql 第二种 二.链接,执行sql,关闭(游标) import pymysql user= input('用户名:>> ...