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. 0X01应用程序黑客技术

    前言 该文章主要是讲解了常见的应用程序黑客技术基本概念,包括消息钩取,API钩取,DLL注入,代码注入 天象独行 0X01:消息钩取 原理:在我们通过键盘,鼠标等输入信息过程中,Windows会通过钩 ...

  2. 机器学习基础系列--先验概率 后验概率 似然函数 最大似然估计(MLE) 最大后验概率(MAE) 以及贝叶斯公式的理解

    目录 机器学习基础 1. 概率和统计 2. 先验概率(由历史求因) 3. 后验概率(知果求因) 4. 似然函数(由因求果) 5. 有趣的野史--贝叶斯和似然之争-最大似然概率(MLE)-最大后验概率( ...

  3. Ubuntu开启端口(持久化)

    1.查看已经开启的端口 sudo ufw status 2.打开80端口 sudo ufw allow 3.防火墙开启 sudo ufw enable 4.防火墙重启 sudo ufw reload

  4. Java基础 -4.6

    循环嵌套 乘法口诀表 public static void main(String[] args) { for(int x =1;x<10;x++) { for(int y=1;y<=x; ...

  5. mybaits requestMap与requestType,以及对应的注解

    有的时候不喜欢用xml配置文件,但是xml配置文件的开发使用简单,自己经常要用到: 因为代码维护的缘故,有的时候看别人的代码,可能存在大量的mappper文件,也不是你想用注解就用注解的: 下面我还是 ...

  6. Tarjan算法与割点割边

    目录 Tarjan算法与无向图的连通性 1:基础概念 2:Tarjan判断割点 3:Tarjan判断割边 Tarjan算法与无向图的连通性 1:基础概念 在说Tarjan算法求解无向图的连通性之前,先 ...

  7. canvas画扇形、饼图

    画扇形的方法 方法一:起始角度是0,那么第一条线就是line(r,0),通过旋转扇形的角度,第二条线就是line(r,0) //圆弧 ctx.save(); ctx.translate(100, 10 ...

  8. Charles + Android 抓取Https数据包 (适用于Android 6.0及以下)

    通过Charles代理,我们能很轻易的抓取手机的Http请求,因为Http属于明文传输,所以我们能直接获取到我们要抓取的内容.但是Https内容本身就是加密的,这时我们会发现内容是加密的了.本文我们来 ...

  9. Centos7 使用yum安装MariaDB与MariaDB的简单配置与使用

    一.mariadb的安装 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜 ...

  10. 8年经验面试官详解 Java 面试秘诀

      作者 | 胡书敏 责编 | 刘静 出品 | CSDN(ID:CSDNnews) 本人目前在一家知名外企担任架构师,而且最近八年来,在多家外企和互联网公司担任Java技术面试官,前后累计面试了有两三 ...