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

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 ≤ ik) 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 ≤ ik).

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%A=a   ①

x%B=b   ②

现在给出两种合并的方法:

1) 要把①②式合并成    x%C=c    ③         易知C一定是A和B的最小公倍数的倍数,否则不可能同时满足①②两式。

这里我们取C为A,B的最小公倍数,设d=gcd(A,B),则C=A*B/d.

接下来我们只要求出余数c即可,假设p是方程组①②的其中一个解

因为③是①②两式的合并方程,所以p也是③的解,所以可以得到c=p%C

接下来的问题就是怎么求出方程组①②的其中一个解。

首先,满足方程组①的最小解显然就是x=a

然后满足①②的解就是  (a+kA)%B=b,其中x=a+kA(k为任意自然数)

这个方程很明显可以用扩展欧几里德算法即可求得x,这样就完成了两个方程的合并

当所有的同余方程合并成一个方程 x%G=g  时候,g即为最终的最小解。。


 #include <iostream>
#include <math.h>
using namespace std;
#define ll long long int
ll funa(ll a,ll b)
{
if(b==) return a;
return funa(b,a%b);
}
void fun(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return ;
}
fun(b,a%b,x,y);
ll t=x;
x=y;
y=t-(ll)(a/b)*y;
}
int main()
{
ll n;
while(cin>>n)
{
int i;
ll a[n][];
for(i=;i<n;i++)
cin>>a[i][]>>a[i][];
for(i=;i<n;i++)
{
ll z=funa(a[i-][],a[i][]);
if((a[i][]-a[i-][])%z!=)
break;
ll x,y;
fun(a[i-][],a[i][],x,y);
x=x*(a[i][]-a[i-][])/z;
x=(x%(a[i][]/z)+a[i][]/z)%(a[i][]/z);
x=x*a[i-][]+a[i-][];
a[i][]=a[i-][]*a[i][]/z;
a[i][]=x;
}
if(i>=n)
cout<<a[n-][]<<endl;
else cout<<-<<endl;
} } //(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n 。


												

poj2891非互质同余方程的更多相关文章

  1. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...

  2. HDU5668 Circle 非互质中国剩余定理

    分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然 ...

  3. POJ 2891- Strange Way to Express Integers CRT 除数非互质

    题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...

  4. 数学--数论--HDU1825(积性函数性质+和函数公式+快速模幂+非互质求逆元)

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  5. hdu 1573 X问题 (非互质的中国剩余定理)

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

  6. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  7. LA 3720 高速公路(互质判斜率)

    https://vjudge.net/problem/UVALive-3720 题意: 有一个n行m列的点阵,问一共有多少条非水平非垂直的直线至少穿过其中的两个点. 思路: 没思路的题. 首先枚举矩形 ...

  8. as+bt=1是ab两数互质的充要条件

    [as+bt=1是ab两数互质的充要条件] 充分性,as+bt=1 => (a,b)=1: 因为as+bt=1,设c=(a,b),则c整除a和b,所以c整除as+bt,即c整除1,所以c=1,即 ...

  9. HDU3579Hello Kiki(中国剩余定理)(不互质的情况)

    One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...

随机推荐

  1. 基于moco的mock server 简单应用 来玩玩吧

    提起mock大家应该就知道是干嘛用的了,再次再介绍一种简单的方式,基于moco的mock server.步骤很简单: 1. 首先,要下载个moco的jar0_1482402640757_moco-ru ...

  2. SQL Server 2014 64位版本链接32位Oracle数据库

    问题背景: 刚进入公司不久的BI新手,最近接部门的一个交接项目,需要在SQL Server上通过openquery来获取Oracle数据库的数据.各种配置,各种设置折腾了一周之久.在此,将自己的问题解 ...

  3. Android事件传递机制详解及最新源码分析——Activity篇

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 在前两篇我们共同探讨了事件传递机制<View篇>与<ViewGroup篇>,我们知道View触摸事件是ViewGroup传递 ...

  4. SpringMVC入门第二天

    SpringMVC第二天 1.   回顾 1.Springmvc介绍? Springmvc是Spring公司 2.Springmvc入门程序 第一步:Web工程 第二步:导Jar包 第三步:web.x ...

  5. https加密实现

    author:JevonWei 版权声明:原创作品 在httpd安装完成的基础上实现https加密 安装mod_ssl软件包 yum -y install mod_ssl http -M 显示mod_ ...

  6. C语言中复数运算及调用blas,lapack中复数函数进行科学计算

    C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计 ...

  7. jQuery常用工具方法

    前面的话 jQuery提供一些与元素无关的工具方法,不必选中元素,就可以直接使用这些方法.如果理解原生javascript的继承原理,那么就能理解工具方法的实质.它是定义在jQuery构造函数上的方法 ...

  8. Adobe Flash Player 因过期而遭遇阻止怎么办

    百度搜索"adobe flash player ppapi "并找到搜索结果中包含www.adobe.com的网址进行在线下载安装即可搞定这个问题[注意要对应你电脑系统中的浏览器, ...

  9. 转:H2 入门

    H2 Database做为轻量级的内嵌数据库,功能十分强大,而且运行时只需要一个jar包即可,下表是官网的描述: 更详细的对比见官网页面: http://www.h2database.com/html ...

  10. 201521123036 《Java程序设计》第7周学习总结

    本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 answer:用于判断Collect ...