天天看点

B. Weakened Common Divisor(筛质因子)

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;
}