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. iOS马甲包上架总结

    https://www.jianshu.com/p/da0a259338ea iOS马甲包上架首先明白一点,这个上架的app马甲包一定是不合规的.不然也不会使用马甲包上架. 上架过程中遇到的坑. 因为 ...

  2. ubuntu 18.04 上安装 docker

    命令安装 docker 1.直接从 ubuntu 仓库安装,打开终端,输入: 2.启动 docker 服务  . 设置开机自启动 docker 服务 3.免 sudo 配置:

  3. js中怎么把类数组转化为数组

    说起伪数组,首先想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使用Ar ...

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:在 <tbody> 内的任一行启用鼠标悬停状态

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

  5. 什么是Socket:

    先了解一些前提: 网络由下往上分为 物理层 .数据链路层 . 网络层 . 传输层 . 会话层 . 表现层 和 应用层.通过初步了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对 ...

  6. Python函数-1 range()函数

    range()函数 在其他语言中,如果想要循环一个变量从1到100,要怎么写呢? for(i = 1,i<=100,i++){} python怎么实现这个功能呢?python设计了range() ...

  7. 第5节 Actor实战:1 - 6

    10.3.   Actor实战 10.3.1.    第一个例子 怎么实现actor并发编程: 1.定义一个class或者是object继承Actor特质,注意导包import scala.actor ...

  8. 异常 日志-<多重catch语句>

    try{ }catch(){ }catch(){ }

  9. 跳转连接转base64

    //配置H5连接 @Value("${h5.domain}") private String h5Domain; // 校验商家端权限 public String checkSta ...

  10. [经验] Linux 怎么连接 Xshell?

    (1) 首先, 你要先有一个可以正常运行的 Linux 系统, 当然一般情况下我们是把这个系统放在虚拟机里的, 我所使用的是 ubuntu-18.04.2-live-server-amd64.iso ...