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.

思路:解同余方程,中国剩余定理,由于每个余数不一定互质,就需要两两合并方程组,给出代码与参考博客:

typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; LL r[maxm], a[maxm]; void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d) {
if(!b) {
d = a, x = , y = ;
} else {
ex_gcd(b, a%b, y, x, d);
y -= x*(a/b);
}
} LL inv(LL t, LL p) {
LL x, y, d;
ex_gcd(t, p, x, y, d);
return d == ?(x%p+p)%p:-;
} LL gcd(LL a, LL b) {
return b?gcd(b, a%b):a;
} PLL linear(LL r[], LL a[], int n) { // x = r[i] (moda[i])
LL x = , m = ;
for(int i = ; i < n; ++i) {
LL A = m, B = r[i] - x, d = gcd(a[i], A);
if(B % d != ) return PLL(, -);
LL t = B/d * inv(A/d, a[i]/d) % (a[i]/d);
x = x + m*t;
m *= a[i]/d;
}
x = (x % m + m) % m;
return PLL(x, m);
} int main() {
int n;
while(scanf("%d", &n) != EOF) {
for(int i = ; i < n; ++i) {
scanf("%lld%lld", &a[i], &r[i]);
}
PLL ans = linear(r, a, n);
if(ans.second == -) printf("-1\n");
else printf("%lld\n", ans.first);
}
return ;
}

https://www.cnblogs.com/linyujun/p/5199415.html

https://blog.csdn.net/acdreamers/article/details/8050018

Day7 - E - Strange Way to Express Integers POJ - 2891的更多相关文章

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

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

  2. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  3. poj——2891 Strange Way to Express Integers

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

  4. [POJ 2891] Strange Way to Express Integers

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

  5. poj Strange Way to Express Integers 中国剩余定理

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

  6. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  7. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  8. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  9. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

随机推荐

  1. 笔记-Python-性能优化

    笔记-Python-性能优化 1.      开始 1.1.    python性能差么? 做一个判断前,先问是不是. python运行效率低是事实. 1.2.    为什么? 原因: Python是 ...

  2. GO判断输入

    判断用户密码输入: package main import"fmt" func main(){ var a int var b int fmt.Printf("请输入密码 ...

  3. windows网络编程-C语言实现简单的UDP协议聊天

    与TCP协议下编写服务端程序代码类似,但因为是无连接的形式,所以不需要监听. 这次,我用了一点不同的想法:我建立一个服务端,用了两个端口和两个套接字,把服务端作为一个数据转发的中转站,使得客户机之间进 ...

  4. Flask - 中间件

    其实就是封装旧酒,装进新瓶,自己再加点料.留坑,还没有用到. Flask的请求扩展就是Django的中间件.Django的中间件不是Flask的中间件 from flask import Flask ...

  5. vue 父组件调用子组件方法简单例子(笔记)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. linux磁盘扩容常见问题

    1.对于云主机可以对硬盘进行在线扩容,如果不方便重启服务器,可以键入以下命令系统能够马上识别新增空间: echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/de ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. HTML相关知识点

    标签: 块元素:可以设置宽高,div, 行内元素:不可以设置宽高,span,image, display:inline;//转换成行内元素 display:block;//转换成块元素 display ...

  9. Django:使用django自带的登录模块登录后会默认登录到 /accounts/profile 下的问题

    django settings中LOGIN_REDIRECT_URL默认重定向到/accounts/profile下,可通过配置修改

  10. for 循环遍历数据动态渲染html

    本案例通过ajax动态获取数据,然后遍历出数据渲染html小心踩坑:因为有时候不注意,渲染页面的时候只能输出最后一个数据所以正确写法为下:如果AJAX数据请求成功的情况下: html <div ...