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. LDA线性分析推广到多分类

    感谢皮果提的文章: http://blog.csdn.net/itplus/article/details/12038441 http://blog.csdn.net/itplus/article 皮 ...

  2. 在Ubuntu上安装Qt5.2.0

    分类: QT2013-12-16 14:44 3171人阅读 评论(0) 收藏 举报 QT官方站点的文档有点老.今天,我尝试着在我的Ubuntu 13.10上安装Qt 5.2.0.下面是我的步骤: 1 ...

  3. JS -- Unexpected trailing comma

    Unexpected trailing comma 后面多了一个逗号

  4. linux下添加动态链接库路径、动态库加载等方法

    linux下添加动态链接库路径的方法 2017年01月20日 10:08:17 阅读数:5596   Linux共享库路径配置 Linux下找不到共享库文件的典型现象为明明已经安装某个软包(如libn ...

  5. 利用socket实现聊天-Android端核心代码

    实体类与服务端报错一致,包括包名 package loaderman.im.bean; /** * 一个聊天消息的JavaBean * * */ public class ChatMsgEntity ...

  6. numpy中flatten学习笔记

    ndarray.flatten() 用法 用于返回一个折叠成一维的数组.该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的. 例子 # coding=utf-8 fro ...

  7. c++ string构造函数学习

    #include <iostream>#include <string> using namespace std; int main(){ string a1; cout &l ...

  8. 使用iptables为docker容器动态添加端口映射

    1.将当前iptables的配置写入保存到/etc/sysconfig/iptables 2.保存 /etc/init.d/iptables sava 3.修改iptables配置(vi /etc/s ...

  9. PowerDesigner设置code和name不联动的方法

    按照如下设置即可: 具体步骤:菜单:Tools--General Options--Name to Code mirroring的复选框不要选中.

  10. sed例子

    以care.log这个log文件为例, care.log: 05:44:31,816 DEBUG RawAggregationWorker:70 - LTS is working on Raw Dat ...