Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 17321   Accepted: 5828

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

解同于方程组:

x≡r1(moda1)
x≡r2(moda2)
......
x≡rn(modan)
其中模数不一定互质。

题解

若模数两两互质,我们可以用中国剩余定理来解。 
这里我们先考虑两个方程:

    x≡r1(moda1)
    x≡r2(moda2)
我们可以写成:
     x+y1a1=r1
     x−y2a2=r2
相减得:y1a1+y2a2=r1−r2也就是ax+by=m的形式。 
这是可以用扩展欧几里德解的。 
若gcd(a,b)|m那么方程就无解,直接输出-1。 (如果m%gcd(a,b)!=0无解)
否则我们可以解出上式的y1。回带得到一个特解x0=r1−y1a1。 
通解可以写成x=x0+k∗lcm(a1,a2)也就是x≡x0(modlcm(a1,a2))。 
这样我们就将两个方程合并为了一个。
重复进行以上操作,我们最终能将n个方程全部合并,再用扩展欧几德得解出来就好了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
ll a[],r[];
int n; ll exgcd(ll a,ll b,ll &x,ll &y){
if(b == ){
x = ;
y = ;
return a;
}
ll d = exgcd(b,a%b,x,y);
ll tmp = x;
x = y;
y = tmp - a/b*y;
return d;
} ll solve(){
ll M = a[],R = r[],x,y;
for(int i=;i<=n;i++){
ll d = exgcd(M,a[i],x,y);
if((R-r[i])%d!=){//无解
return -;
}
x = (R-r[i])/d*x%a[i];//这才是真正的x,记得模a[i]
R -= x*M;//特解x0,新的余数
M = M/d*a[i];//新的模数
R %= M;
}
return (R%M+M)%M;//确保R不为负数
} int main(){
while(cin >> n){
for(int i=;i<=n;i++){
cin >> a[i] >> r[i];
}
ll ans = solve();
cout << ans << endl;
}
return ;
}
												

数论 线性同余方程的应用 poj2891的更多相关文章

  1. 数论之同余性质 线性同余方程&拔山盖世BSGS&中国剩余定理

    先记录一下一些概念和定理 同余:给定整数a,b,c,若用c不停的去除a和b最终所得余数一样,则称a和b对模c同余,记做a≡b (mod c),同余满足自反性,对称性,传递性 定理1: 若a≡b (mo ...

  2. 数论 - n元线性同余方程的解法

    note:n元线性同余方程因其编程的特殊性,一般在acm中用的很少,这里只是出于兴趣学了一下 n元线性同余方程的概念: 形如:(a1*x1+a2*x2+....+an*xn)%m=b%m       ...

  3. POJ2115:C Looooops(一元线性同余方程)

    题目: http://poj.org/problem?id=2115 要求: 会求最优解,会求这d个解,即(x+(i-1)*b/d)modm;(看最后那个博客的链接地址) 前两天用二元一次线性方程解过 ...

  4. codeforces 710D Two Arithmetic Progressions(线性同余方程)

    题目链接: http://codeforces.com/problemset/problem/710/D 分析:给你两个方程 a1k + b1 and a2l + b2,求在一个闭区间[L,R]中有多 ...

  5. 初等数论-Base-2(扩展欧几里得算法,同余,线性同余方程,(附:裴蜀定理的证明))

    我们接着上面的欧几里得算法说 扩展欧几里得算法 扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足贝祖等式\(^①\): ax+by = gcd(a, b) =d(解一定存在,根据数论中的 ...

  6. POJ2115 C Looooops(线性同余方程)

    无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...

  7. POJ1061 青蛙的约会(线性同余方程)

    线性同余方程$ ax \equiv b \pmod n$可以用扩展欧几里得算法求解. 这一题假设青蛙们跳t次后相遇,则可列方程: $$ Mt+X \equiv Nt+Y \pmod L$$ $$ (M ...

  8. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

  9. poj2115-C Looooops -线性同余方程

    线性同余方程的模板题.和青蛙的约会一样. #include <cstdio> #include <cstring> #define LL long long using nam ...

随机推荐

  1. Code blocks返回错误代码:Process returned -1073741819 (0xC0000005)

    循环语句访问链表时,返回了错误代码: 逐项排查后,发现是由while循环引起的: 附上出错代码: do{ L=L->post; printf("%05d %d %05d\n" ...

  2. spring实战学习笔记(一)spring装配bean

    最近在学习spring boot 发现对某些注解不是很深入的了解.看技术书给出的实例 会很疑惑为什么要用这个注解? 这个注解的作用?有其他相同作用的注解吗?这个注解的运行机制是什么?等等 spring ...

  3. 新IT运维时代 | Docker运维之最佳实践-上篇

    容器技术的发展可以分为两个阶段,第一个阶段聚焦在IaaS层,仅仅把容器当做更轻量级虚拟机来使用,解决了应用运行时进程级资源隔离的问题:随着Docker的出现,容器虚拟化才有了统一的平台,由此容器技术发 ...

  4. idea 2018.3.4安装破解

    电脑环境:win10 64位 1.idea官网下载: 链接:https://www.jetbrains.com/idea/,如下图: 2.JDK官网下载: 链接:https://www.oracle. ...

  5. luogu1373_小a和uim之大逃离 多维dp

    传送门 巧妙之处在于dp的设计只用设计差值即可,因此不会mle,枚举的顺序问题也解决了 #include <bits/stdc++.h> using namespace std; #def ...

  6. 使用top查看进程和系统负载信息

    引言      使用top命令,可以查看正在运行的进程和系统负载信息,包括cpu负载.内存使用.各个进程所占系统资源等,top可以以一定频率更新这些统计信息.下面我们来学习top命令的具体使用方法. ...

  7. 谈谈我对Ext的认识,元芳,你怎么看

    实用Ext第一步当然是引用jar包啦. 下载地址 在页面上加上div用于显示这也是必须的 <div id='loginpanel' ></div> 在js中我们肯定需要将Ext ...

  8. 《深入理解Java虚拟机》- Java虚拟机是如何加载Java类的?

    Java虚拟机是如何加载Java类的?  这个问题也就是面试常问到的Java类加载机制.在年初面试百战之后,菜鸟喜鹊也是能把这流程倒背如流啊!但是,也只是字面上的背诵,根本就是像上学时背书考试一样. ...

  9. c# NPOI 导出23万条记录耗时12秒

    先上测试代码: string connectionString = "Server=localhost;Initial Catalog=******;User ID=sa;Password= ...

  10. SpringIoC和SpringMVC的快速入门

    更多内容,欢迎关注微信公众号:全菜工程师小辉~ Spring的优势? 降低了组件之间的耦合性 ,实现了软件各层之间的解耦 可以使用容易提供的众多服务,如事务管理,消息服务等 容器提供单例模式支持 容器 ...