B. Weakened Common Divisor(筛质因子)

本文探讨了数学家Ildar提出的一种新颖概念——弱化公因数(WCD),并提供了一种高效的算法来计算给定整数对集合的WCD。通过筛选和验证质因数的方法,确保每个整数对中至少有一个数包含该因数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

3
17 18
15 24
12 15

output

6

input

2
10 16
7 17

output

-1

input

5
90 108
45 105
75 40
165 175
33 30

output

5

题目大概:

给出n对数,问是否存在一个数,使得没对数都能找出至少一个数的因子包含该数。

思路:

筛出第一对的质因子,和它本身。

循环暴力判断这些数   是否是剩下的数对的因子。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5e5+100;
const int N=1e7;
const int INF=1e9+7;
const int mod=1e9+7;
int a[maxn];
int b[maxn],c[maxn];
int ans=0;

void go(int x)
{
    int i;
    for(i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            c[ans++]=i;
            while(x%i==0)x/=i;
        }
    }
    if(x>1)c[ans++]=x;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    go(a[1]);
    go(b[1]);
    c[ans++]=a[1];
    c[ans++]=b[1];
    sort(c,c+ans);
    ans=unique(c,c+ans)-c;
    int flag=0;
    int id=0;
    for(int i=0;i<ans;i++)
    {
        int sum=0;
        for(int j=2;j<=n;j++)
        {
            if(a[j]%c[i]==0||b[j]%c[i]==0)
            {
                sum++;
            }
        }
        if(sum==n-1)
        {
            flag=1;
            id=c[i];
            break;
        }
    }
    if(flag)
    {
        printf("%d\n",id);
    }
    else printf("-1\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值