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.

线性同余方程组,终于自己写了一遍。棒棒哒。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
void Ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(b==){ d=a; x=; y=; return ;}
Ex_gcd(b,a%b,d,y,x); y-=a/b*x;
}
int main()
{
ll c1,c2,c,a,b,d,x,y,n;
while(~scanf("%lld",&n)){
bool Flag=false;
scanf("%lld%lld",&a,&c1);
for(int i=;i<=n;i++) {
scanf("%lld%lld",&b,&c2);
if(Flag) continue; c=c2-c1;
Ex_gcd(a,b,d,x,y);
if(c%d!=) { printf("-1\n"); Flag=true;}
x=((c/d*x)%(b/d)+b/d)%(b/d);//最小正单元
c1=a*x+c1;a=a*b/d;
}
if(!Flag) printf("%lld\n",c1);
} return ;
}

POJ2891Strange Way to Express Integers (线性同余方程组)的更多相关文章

  1. HDU3579:Hello Kiki(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...

  2. HDU1573:X问题(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...

  3. HDU1573 X问题【一元线性同余方程组】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...

  4. AcWing 204. 表达整数的奇怪方式 (线性同余方程组)打卡

    给定2n个整数a1,a2,…,ana1,a2,…,an和m1,m2,…,mnm1,m2,…,mn,求一个最小的整数x,满足∀i∈[1,n],x≡mi(mod ai)∀i∈[1,n],x≡mi(mod  ...

  5. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  6. poj2891(线性同余方程组)

    一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组. Strange Way to Express Integers Time Limit: 1000MS   Memory Limi ...

  7. POJ-2891-Strange Way to Express Integers(线性同余方程组)

    链接: https://vjudge.net/problem/POJ-2891 题意: Elina is reading a book written by Rujia Liu, which intr ...

  8. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  9. POJ2891:Strange Way to Express Integers(解一元线性同余方程组)

    写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...

随机推荐

  1. sublime中如何安装vue.js插件,并使代码高亮显示

    前提概要: sublime的下载地址:http://www.sublimetext.com/ notepad++下载地址:https://notepad-plus-plus.org/ .vue的文件在 ...

  2. github 答题

    头脑王者 / 百万英雄 / 冲顶大会 / 芝士超人 自动答题:https://github.com/cxs1994/python_answer 头脑王者:https://github.com/sear ...

  3. Android自定义View分析

    一.基本步骤 1.自定义View的属性 2.在View的构造方法中获取自定义属性 3.重写onMesure方法(非必须) 4.重写onDraw方法 二.具体实现 1.自定义View的属性,首先在res ...

  4. 百度地图API简介

    百度地图API简介 在此申明不是我写的,用的是别人的,仅限自己学习 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能 ...

  5. sublime text 3 语法检查插件

    第一种方法:有点卡 先去下载对应的开发环境,安装到本地,例如php. 从Pakage Control中安装sublimelinter和sublimelinter-*,*为所用的语言,例如sublime ...

  6. 4.AutowireCapableBeanFactory 自动装配工厂

    AutowireCapableBeanFactory 根据名称:自动装配的BeanFactory,其实也是对BeanFactory的增强 源代码: /* * Copyright 2002-2016 t ...

  7. world embedding 嵌入

    http://papers.nips.cc/paper/5477-neural-word-embedding-as-implicit-matrix-factorization 嵌入  |  Tenso ...

  8. [ZJOI2006]三色二叉树

    [ZJOI2006]三色二叉树 BZOJ luogu 分3种颜色讨论转移一下 #include<bits/stdc++.h> using namespace std; const int ...

  9. hadoop2.2.0安装需要注意的事情

    今天在安装hadoop2.2.0时遇到若干问题,解决这些问题有些心得,记录下来以备不时之需. 问题1.master和slave之间不能相互ssh免密码登陆. 问题表象此处略过,直接说解决办法: 1.查 ...

  10. ALV 红绿灯编写

    [转http://lz357502668.blog.163.com/blog/static/164967432012417102133216/]ALV 指示燈 編寫 1,在內表中申請一個字段. 該字段 ...