Strange Way to Express Integers

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 a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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 模数不互质的中国剩余定理的更多相关文章

  1. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  2. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  3. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  4. hdu 1573 X问题 不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 中国剩余定理模数不互质的情况(poj 2891

    中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...

  6. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  7. POJ 1006:Biorhythms 中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Des ...

  8. acm数论之旅--中国剩余定理

    ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)   中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...

  9. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

随机推荐

  1. python 获取昨天的日期

    from datetime import timedelta, datetime yesterday = datetime.today()+timedelta(-1) yesterday_format ...

  2. 深入理解Flink ---- End-to-End Exactly-Once语义

    上一篇文章所述的Exactly-Once语义是针对Flink系统内部而言的. 那么Flink和外部系统(如Kafka)之间的消息传递如何做到exactly once呢? 问题所在: 如上图,当sink ...

  3. 如何调试JS查看异常信息

    如果页面上有错误,html页面的控制台会报错,可以查看报错信息,找到对应的行,找到出错的位置.也可以通过editplus运行调试,editplus会以弹框的形式出现提示,哪行的什么位置什么错误,需要记 ...

  4. Hadoop 部署之环境准备(一)

    目录 一.软硬件规划 二.主机名解析 三.配置 SSH 互信 四.创建用户 五.JDK 的安装 一.软硬件规划 ID 主机类型 主机名 IP 应用软件 操作系统 硬件配置 1 物理机 namenode ...

  5. 怎么理解linux作业(job),与进程(process)的关系

    1.相关概念: shell :命令解释器,其实就是一个脚本语言解释器,有很多种(bash,ash,tcsh等),最常用的是bash. job(作业): 是相对shell 来说的,在shell中执行一条 ...

  6. 小程序签名MD5加密

    最近小程序需求一个签名加密,要把请求参数按键值排序并转化为字符串,然后进行MD5加密. //时间戳 var timestamp = (Date.parse(new Date()))/1000;//签名 ...

  7. Day04:循环结构(while、do-while、for)

    Java 循环结构 - while ,do...while,for 反复执行一段相同或相似代码的格式. 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java ...

  8. [BAT] SetX 永久设置环境变量

    SetX 有三种使用方式: 语法 1: SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M] 语法 2: SETX [/ ...

  9. 【编程开发】MD5和RSA

    MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. (1)MD5 MD5(单向散列算法)的全称是Message-Di ...

  10. 洛谷 题解 P4158 【[SCOI2009]粉刷匠】

    状态: dp[i][j][k][0/1]: 到达第i行时, 到达第j列时, 刷到第k次时, 这一格有没有刷对 转移 换一块木板时肯定要多刷一次 dp[i][j][k][0]=max(dp[i-1][m ...