poj 2891 模数不互质的中国剩余定理
Description
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
- Line 1: Contains the integer k.
- Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
题意:
就是给你n组数据
每一组数据a,b 表示 x%a==b
求这n组数据的公共的最小x的解
题解:
中国剩余定理中的不互质的模线性方程组
由不互质的模线性方程组
x%r1=a1 1
x%r2=a2 2
x%r3=a3 3
x%r4=a4 4
......
由 1 2式子得到:
x = r1*k1 + a1
x = r2*k2 + a2
联立:
r1*k1 + a1 =r2*k2 + a2
得到
r1*k1 + r2*k2 = a2 - a1 (其中k2的正负可变)
令g=gcd(r1,r2)
那么上叙方程同时除以g后
r1*k1 / g + r2*k2 / g = (a2 - a1 )/ g
由扩展欧几里得可以得到k1,则有
x1 = r1*k1 + a1 5
x1表示1 2式子得到的r1 和 让r2的最大公约数
上ac代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
ll a[100005],r[100005];
int n; ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0) return x=1,y=0,a;
ll tmp=exgcd(b,a%b,y,x);
y-=a/b*x;
return tmp;
} ll solve(){
ll M=a[1],R=r[1],x,y,d;
for(int i=2;i<=n;i++){
d=exgcd(M,a[i],x,y);
if((R-r[i])%d!=0) return -1;
x=(R-r[i])/d*x%a[i];
R-=x*M;
M=M/d*a[i];
R%=M;
}
return (R%M+M)%M;
} int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i],&r[i]);
printf("%lld\n",solve());
}
return 0;
}
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm> using namespace std; typedef long long ll; ll a[100005],r[100005]; int n; ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0) returnx=1,y=0,a; ll tmp=exgcd(b,a%b,y,x); y-=a/b*x; return tmp; } ll solve(){ ll M=a[1],R=r[1],x,y,d; for(int i=2;i<=n;i++){ d=exgcd(M,a[i],x,y); if((R-r[i])%d!=0) return -1; x=(R-r[i])/d*x%a[i]; R-=x*M; M=M/d*a[i]; R%=M; } return (R%M+M)%M; } int main(){ while(~scanf("%d",&n)){ for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i],&r[i]); printf("%lld\n",solve()); } return0; }
poj 2891 模数不互质的中国剩余定理的更多相关文章
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- hdu 1573 X问题 两两可能不互质的中国剩余定理
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- hdu 1573 X问题 不互质的中国剩余定理
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...
- POJ 1006 中国剩余定理
#include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...
- POJ 1006:Biorhythms 中国剩余定理
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 121194 Accepted: 38157 Des ...
- acm数论之旅--中国剩余定理
ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯) 中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...
- (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)
前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...
随机推荐
- DQL:查询表中数据
1. 基础查询 (1) 查询整表 SELECT * FROM 表名; -- 不推荐使用"*",不方便阅读 (2) 选择性查询 SELECT 列名,列名,列名 FROM 表名; (3 ...
- Parameter 'list1' not found. Available parameters are [list] 解析
在使用foreach语句时会经常报Parameter ‘ordersList’ not found. Available parameters are [list] 解析这个错误,造成这个错误的主要原 ...
- js的dom测试及实例代码
js的dom测试及实例代码 一.总结 一句话总结: 1.需要记得 创建 标签和创建文本节点都是document的活:document.createTextNode("Rockets的姚明&q ...
- CIEDE2000色差公式相关
色差公式发展的三个重要的阶段:1976年以前(CIELAB和CIELUV的采用).1976年到2001年(CIEDE2000色差公式的推荐).2001年以后. 国际照明委员会1998年成立了技术委员会 ...
- Tomca的启动与关闭
点击startup.bat启动,遇到一闪而过的问题,可能尚未配置JAVA_HOME 8080端口被占用导致启动失败 关闭Tomcat的三种方式 * 点击x (不推荐) * 双击shutdown.bat ...
- ROS 常用命令
1.查看网卡接口: /interface print 2.给网口添加IP地址: /ip address add address=192.168.1.254/24 interface=ether1 3 ...
- git服务器搭建---便签做备注
今天,简单搭建了一下git服务器.发现一篇文章写的挺好的 http://www.cnblogs.com/trying/archive/2012/06/28/2863758.html 并简单和廖雪峰的结 ...
- c++ for_each
#include<iostream>#include<algorithm>#include<vector>using namespace std; int main ...
- iOS——偏好设置的创建,数据写入与读取
NSUserDefaults与NSDictinary? 应用通过NSUserDefaults用键值对的方式来读取和保存偏好设置数据,与通过键从NSDictionary对象中获取数据一样,不同之处在于N ...
- 深入理解Android消息机制
在日常的开发中,Android 的消息机制作为系统运行的根本机制之一,显得十分的重要. 从 Handler 发送消息开始 查看源码,Handler的post.send方法最终都会走到 public f ...