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. Ubuntu16.04+CUDA8.0+CUNN5.1+caffe+tensorflow+Theano

    title: Ubuntu 16.04+CUDA8.0+CUNN5.1+caffe+tensorflow+Theano categories: 深度学习 tags: [深度学习框架搭建] --- 前言 ...

  2. TCON板新选择--NCS8807 LVDS转mLVDS芯片

    NCS8807 LVDS-to-mLVDS w/ Scaler (4K TCON w/ Scaler) General Description NCS8807 is an LVDS 4K TCON w ...

  3. 一起来学linux:shell script(一)关于变量

    p { margin-bottom: 0.25cm; line-height: 120% } (一)首先来看什么是变量,在shell中打印出变量采用的是echo $path或者echo ${path} ...

  4. Java 得到磁盘以及内存信息

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt217 1.得到磁盘信息 File[] files = File. listR ...

  5. .NET及.NET Core系统架构

    三层及多层架构 Multitier Architecture ASP.NET N-Tier Architecture Schema Visual Studio N-Tier Example 来源:ht ...

  6. Angular+ionic2 web端 启动程序出现短暂 白屏或黑屏 的处理小妙招

    在ionic2项目启动是会出现短暂的白屏或者黑屏,虽然很短,但是用户体验不太好.上网查了相关的资料,都是针对打包APP的解决办法,针对浏览器端使用的项目没有效果,所以自己写了样式,巧妙的避开这个问题, ...

  7. Ubuntu linux安装putty

    Step1 安装Putty $ sudo apt-get install putty Step2 使用Putty Client 查找已经安装完的putty客户端 设置Remote Server的IP地 ...

  8. 图论中DFS与BFS的区别、用法、详解…

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

  9. hashMap和treeMap

    前言 首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.这就是我们平时说的键值对. ...

  10. 201521123008《Java程序设计》第四周学习总结

    1.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.包的命名规范 2.面向对象设计:名词/动词 3.类的设计技巧:一定将属性设计为私有priv ...