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. 自定义列表数据自动循环向下滚动view(类似于通知通报消息)

    首先申明,这个自定义View不是本人写的,大神写的,本人仅限学习一级研究使用 直接上代码吧!后面我再解释一遍 package com.egojit.android.gcoa.views; import ...

  2. c语言三元组

    // Triplet.cpp : 定义控制台应用程序的入口点.//#include "stdio.h"#include "stdlib.h"#define OK ...

  3. IT人和普洱茶

    IT人与普洱茶 作为一个平凡的IT人,在小孩眼中我就像黑客帝国的主角一样了不起:在亲戚眼中我是在写字楼做办公室吹空调的人:在朋友眼中我就是一个会写代码.掌握高科技术的人:在女友眼中我是一个在名企工作的 ...

  4. Mysql无法创建函数解决办法

    执行: set global log_bin_trust_function_creators =1; 原文参照:http://www.cnblogs.com/xd502djj/archive/2012 ...

  5. maven assembly 配置详解

    Maven Assembly插件介绍 博客分类: 项目构建   你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembly插件能帮你构建一个完整的发布包. Assembl ...

  6. Cloneable 和clone的区别和联系

    设计模式----原型模式时候,涉及到的复制克隆, Cloneable 接口,内部是没有任何方法的, 这个接口其实是一个标记性的接口,和Serializable是一样的,都是标记使用, 在类实现了这个C ...

  7. Action三种编写方式

    1.     创建普通类不实现接口与继承类 2.     实现Action接口 3.     继承ActionSupport类(常用)

  8. 聊聊数据库~6.SQL运维中篇

    上篇回顾:https://www.cnblogs.com/dotnetcrazy/p/10810798.html#top 1.6.5.MySQL日志相关 本文的测试环境:MySQL5.7.26.Mar ...

  9. vim复制多行

    比如我要复制从第1行到第5行的数据,复制到第9行 光标移到第5行任意位置,输入ma光标移到第1行任意位置,输入y'a(这一定要打这个“'”单引号,否则就进入“INSERT”状态了光标移到需要复制的行, ...

  10. HDU - 5695 Gym Class 【拓扑排序】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...