Weakened Common Divisor
time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers.

For a given list of pairs of integers (a1,b1)(a1,b1), (a2,b2)(a2,b2), ..., (an,bn)(an,bn) their WCD is arbitrary integer greater than 11, such that it divides at least one element in each pair. WCD may not exist for some lists.

For example, if the list looks like [(12,15),(25,18),(10,24)][(12,15),(25,18),(10,24)], then their WCD can be equal to 22, 33, 55 or 66 (each of these numbers is strictly greater than 11 and divides at least one number in each pair).

You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently.

Input

The first line contains a single integer nn (1≤n≤1500001≤n≤150000) — the number of pairs.

Each of the next nn lines contains two integer values aiai, bibi (2≤ai,bi≤2⋅1092≤ai,bi≤2⋅109).

Output

Print a single integer — the WCD of the set of pairs.

If there are multiple possible answers, output any; if there is no answer, print −1−1.

Examples
input

Copy
3
17 18
15 24
12 15
output

Copy
6
input

Copy
2
10 16
7 17
output

Copy
-1
input

Copy
5
90 108
45 105
75 40
165 175
33 30
output

Copy
5
Note

In the first example the answer is 66 since it divides 1818 from the first pair, 2424 from the second and 1212 from the third ones. Note that other valid answers will also be accepted.

In the second example there are no integers greater than 11 satisfying the conditions.

In the third example one of the possible answers is 55. Note that, for example, 1515 is also allowed, but it's not necessary to maximize the output.

题意:有n组数,每组数有两个数,求一个数是所有组数中的两个中一个的因子

分析:分解第一组数得到他们的质因子,如果这些数有解,则这些因子肯定有一个是其他所有组数中至少一个数的因子

  枚举剩下每组数

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll a[maxn], b[maxn];
int main() {
ios::sync_with_stdio(0);
ll n, x, y;
set<ll> s, t;
cin >> n;
for( ll i = 0; i < n; i ++ ) {
cin >> a[i] >> b[i];
}
x = a[0], y = b[0];
for( ll i = 2; i*i <= x; i ++ ) {
if( x%i == 0 ) {
s.insert(i);
while( x%i == 0 ) {
x /= i;
}
}
}
for( ll i = 2; i*i <= y; i ++ ) {
if( y%i == 0 ) {
s.insert(i);
while( y%i == 0 ) {
y /= i;
}
}
}
if( x > 1 ) {
s.insert(x);
}
if( y > 1 ) {
s.insert(y);
}
bool flg = false;
for( ll i : s ) {
bool flag = true;
for( ll j = 1; j < n; j ++ ) {
if( a[j]%i && b[j]%i ) {
flag = false;
break;
}
}
if(flag) {
cout << i << endl;
flg = true;
break;
}
}
if(!flg) {
cout << -1 << endl;
}
return 0;
}

  

最新文章

  1. CS193P - 2016年秋 第三讲 Swift 语言及 Foundation 框架
  2. Refresh recovery area usage data after manually deleting files under recovery area
  3. Oracle ASM diskgroup在主机重启后启动失败
  4. Delphi 的知识体系
  5. 查看JAVA进程中哪个线程CPU消耗最高
  6. 继承ActionSupport,返回INPUT的原因
  7. spfa的SLF优化
  8. JS传参出现乱码(转载)
  9. Ⅵ.spring的点点滴滴--自定义类型转换器
  10. datazen Active Directory AD 配置
  11. Swift - 03 - 整数类型
  12. ckplayer 实现
  13. Python学习之路-Day1-Python基础
  14. Mysql元数据生成Hive建表语句注释脚本
  15. MySQL学习笔记_7_MySQL常用内置函数
  16. IntelliJ IDEA中 todo的使用
  17. sweetalert提示框
  18. 【原创 Hadoop&amp;Spark 动手实践 11】Spark Streaming 应用与动手实践
  19. Linux c time模块函数库
  20. servlet-servlet的简单认识——源码解析

热门文章

  1. Lexical or preprocessor &#39;XXX/XXX.h&#39; issue file not found
  2. Nginx安装(详细版本)
  3. Asp.Net MVC 高级特性(附带源码剖析)
  4. BGP属性控制实验
  5. 什么是redis的缓存雪崩与缓存穿透
  6. GooglePlay新版排行榜接入
  7. 【TCP/IP】ICMP协议
  8. 关于stm32f1使用ST官方DSP库中的FFT方法
  9. let 、const 、var、function声明关键字的新理解
  10. 强烈推荐优秀的Vue UI组件库