codeforces#505--B Weakened Common Divisor

本文介绍了一种名为“弱化公因数”(WCD)的概念,并提供了一个算法来寻找一组整数对的WCD。WCD是指大于1且能整除每对整数中至少一个数的任意整数。文章还给出了具体的实现代码。

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

 

转载于:https://www.cnblogs.com/wyboooo/p/9643376.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值