POJ 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式
任意n个表达式一对对处理,故只需处理两个表达式。
x = a(mod m)
x = b(mod n)
km+a = b (mod n)
km = (a-b)(mod n)
利用扩展欧几里得算法求出k
k = k0(mod n/(n,m)) = k0 + h*n/(n,m)
x = km+a = k0*m+a+h*n*m/(n,m) = k0*m+a (mod n*m/(n,m))
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define LL long long using namespace std;
LL m0, m1, a0, a1; LL Exgcd(LL a,LL b,LL &x,LL &y) // ax+by = (a,b)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL x1,y1,x0,y0;
x0=1; y0=0;
x1=0; y1=1;
x=0; y=1;
LL r=a%b;
LL q=(a-r)/b;
while(r)
{
x=x0-q*x1; y=y0-q*y1;
x0=x1; y0=y1;
x1=x; y1=y;
a=b; b=r; r=a%b;
q=(a-r)/b;
}
return b;
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
scanf("%lld%lld", &m0, &a0);
bool flag = 0;
while(--n)
{
scanf("%lld%lld", &m1, &a1);
LL x, y, tmp;
LL d = Exgcd(m0, m1, x, y);
x = (x%(m1/d)+m1/d)%(m1/d);
tmp = ((a1-a0)%m1+m1)%m1;
if(tmp%d != 0)
flag = 1;
x = x*(tmp/d)%(m1/d);
a0 = (x*m0+a0+m0/d*m1)%(m0/d*m1);
m0 = m0/d*m1;
}
if(flag)
printf("-1\n");
else
printf("%lld\n", a0);
}
return 0;
}
POJ 2891 中国剩余定理的非互质形式的更多相关文章
- 中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- POJ 2891- Strange Way to Express Integers CRT 除数非互质
题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...
- HDU5668 Circle 非互质中国剩余定理
分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- 数学--数论--HDU1825(积性函数性质+和函数公式+快速模幂+非互质求逆元)
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- hdu 1573 X问题 (非互质的中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- poj2891非互质同余方程
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8176 ...
随机推荐
- A class for dynamic icons in Windows
A class for dynamic icons in Windows #include <windows.h> class DynamicIcon {public: DynamicI ...
- A simple windows programm in c
A simple windows programm in c The following programm is a minimal windows program. It opens ...
- cocoon + carrierwave 多图片上传用法
gem 'cocoon' gem 'carrierwave' gem 'mini_magick' 1.图片上传carrierwave配置,github 自己手动添加的配置,没用命令生成 在app下新建 ...
- MySQL 温故知心(三)
MySQL锁概述 相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level loc ...
- appium服务关键字
关键字 描述 实例 automationName 你想使用的自动化测试引擎 Appium (默认) 或 Selendroid platformName 你要测试的手机操作系统 iOS, Android ...
- cocos2dx lua invalid 'cobj' in function 'lua_cocos2dx‘ 躺坑
for 循环中保存了创建的 Node节点到 userdata 数据结构中 再次引用发现 一直报 LUA ERROR: [string ".\framework/cocos2dx/NodeEx ...
- 今天刚接触lua 写写环境配置和基本设置
首先是安装开发工具: Quick: http://www.tairan.com/engines-download 引擎 Sublime Text的下载地址为:http://www.sublimetex ...
- selenium的基本介绍
应吴姑娘(漂亮的姑娘)之邀,加上我师兄(屌丝)和国新(屌丝),组了个四黑小团伙,每周二分享点东西,感觉就是四个辣鸡相互取暖.可惜,今天早上直接是睡过去了,下午都捐给了<白夜追凶>---没办 ...
- win7_32下编译FFmpeg
运行环境: VC2010软件: [附:本文所用软件安装包:http://download.csdn.NET/detail/sinat_36666600/9705438 ...
- 阿里云 Linux 启用465端口发送邮件
阿里云 Linux 启用465端口发送邮件 环境:阿里云 Linux Centos 7.4 x64 注:阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送. 注:确保邮箱开启SMTP服务, ...