Codeforces 1025 - B. Weakened Common Divisor(质因数分解)

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

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

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.

 

思路:对第一组的两个数x1,y1我们质因数分解。然后判断后面每组数x[i],y[i]是否能整除 其质因子集合。

#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<< "----------"<<endl;

const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N =1010;

int n;
int x[MAXN],y[MAXN];
set<int>se;
void get_prime(int n){
    for(int i=2;i*i<=n;i++){
        if(n%i == 0){
            se.insert(i);
            while(n%i == 0) n /= i;
        }
    }
    if(n != 1) se.insert(n);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        clr(x);clr(y);
        se.clear();
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x[i],&y[i]);
        }
        get_prime(x[1]);
        get_prime(y[1]);
        int flag = 1,Flag = 0;
        set<int>::iterator it;
        for(it=se.begin();it!=se.end();it++){
            int temp = *it;
            flag = 1;
            for(int i=2;i<=n;i++){
                if(x[i]%temp !=0 && y[i]%temp !=0){
                    flag = 0;
                    break;
                }
            }
            if(flag){
                printf("%d\n",temp);
                Flag = 1;
                break;
            }
            else continue;
        }
        if(Flag) continue;
        else printf("-1\n");
    }
    return 0;
}

 

    原文作者:B树
    原文地址: https://blog.csdn.net/l18339702017/article/details/81844956
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞