B. 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.

枚举一下第一对数的因子用set存下

注意用素数筛来做不然会超时

也要注意n=1的情况

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std; typedef long long LL;
int n;
const int maxn = 150005;
LL a[maxn], b[maxn];
set<LL> num;
set<LL>::iterator it; void cal(LL x, LL y)
{
for(LL i = 2; i * i <= x; i++){
if(x % i == 0){
num.insert(i);
}
while(x % i == 0){
x /= i;
}
}
if(x > 1)
num.insert(x); for(LL i = 2; i * i <= y; i++){
if(y % i == 0){
num.insert(i);
}
while(y % i == 0){
y /= i;
}
}
if(y > 1){
num.insert(y);
}
} int main()
{
while(scanf("%d", &n) != EOF){
num.clear();
for(int i = 0; i < n; i++){
scanf("%I64d%I64d", &a[i], &b[i]);
}
cal(a[0], b[0]); if(n == 1){
it = num.begin();
printf("%I64d\n", *it);
}
else{
bool ed = false;
for(it = num.begin(); it != num.end(); it++){
bool flag = true;
LL t = *it;
for(int j = 1; j < n; j++){
if((a[j] % t != 0) && (b[j] % t != 0)){
flag = false;
break;
}
}
if(flag){
printf("%I64d\n", t);
ed = true;
break;
}
}
if(!ed){
printf("-1\n");
}
}
}
return 0;
}

codeforces#505--B Weakened Common Divisor的更多相关文章

  1. CF #505 B Weakened Common Divisor(数论)题解

    题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1. 思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘, ...

  2. CF1025B Weakened Common Divisor 数学

    Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  3. Codeforces #505(div1+div2) B Weakened Common Divisor

    题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...

  4. 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor

    [链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. ...

  5. CodeForces - 1025B Weakened Common Divisor

    http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...

  6. codeforces 1025B Weakened Common Divisor(质因数分解)

    题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...

  7. CF1025B Weakened Common Divisor【数论/GCD/思维】

    #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...

  8. CF1025B Weakened Common Divisor

    思路: 首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件. 时间复杂 ...

  9. CF1025B Weakened Common Divisor 题解

    Content 定义 \(n\) 个数对 \((a_1,b_1),(a_2,b_2),(a_3,b_3),...,(a_n,b_n)\) 的 \(\text{WCD}\) 为能够整除每个数对中至少一个 ...

随机推荐

  1. 【转】MFC WM_USER和WM_APP

    WM_USER常量是Windows帮助应用程序定义私有窗口类里的私有消息,通常使用WM_USER+一个整数值,但总值不能超过0x7FFF. #define WM_USER       0x0400 W ...

  2. motion的移植和使用

    说明: motion主页:http://www.lavrsen.dk/foswiki/bin/view/Motion motion下载地址:http://sourceforge.net/project ...

  3. VMWare -- 工作模式

    VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). 桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和 ...

  4. ZABBIX API简介及使用

    API简介 Zabbix API开始扮演着越来越重要的角色,尤其是在集成第三方软件和自动化日常任务时.很难想象管理数千台服务器而没有自动化是多么的困难.Zabbix API为批量操作.第三方软件集成以 ...

  5. Matlab中imread函数使用报错“不应为MATLAB 表达式”分析

    问题描述: 使用imread读取特定路径下的文件时,会提示出错! >> mytest错误: 文件:mytest.m 行:10 列:87不应为 MATLAB 表达式. 出错行: Images ...

  6. mysqldump如何针对某些数据库进行备份?针对某个数据库进行备份?

    需求描述: 通过mysqldump工具对mysql服务器中的某几个数据库进行备份. 或者就对其中的一个数据库进行备份. 操作过程: 1.通过--databases参数后面加上数据库的名字进行备份 [m ...

  7. How to Setup Cordova for Windows 7

    Setup Cordova Text Editor / IDE You may need to prepare an IDE or Editor for working. Here for examp ...

  8. 交换a、b的值temp = a; a = b; b = temp;比a = a^b;b = a^b;a = a^b;快

    先看代码,交换a.b的值十亿次 <span style="font-size:14px;"> int a=222; int b=111; int size = 1000 ...

  9. 工作流JBPM_day01:3-使用JBPM的API添加与执行流程

    工作流JBPM_day01:3-使用JBPM的API添加与执行流程 流程定义画完得到压缩文件--->部署流程定义-->启动流程实例-->查询我的个人任务列表-->办理任务--& ...

  10. Java课后简答题

    1.简述Java的特点. 面向对象.跨平台性.健壮性.安全性.可移植性.多线程性.动态性等. 2.简述JRE与JDK的区别. JRE(Java Runtime Environment,Java运行时环 ...