1025B - Weakened Common Divisor

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/81866192

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.

题意:给n对数据,让你找一个弱公因数,弱公因数大于1,使它是每组任意取出数的公因数。

题解:数论  昨晚不停的wa7,今早一看 这组数据,输出的是2还可以是3,5,刚开始还不理解,后来问了实验室同学了,是每组任意取出的数,对于只有一组数据题意理解有偏差,我以为是一组数据的时候,就要这两个数gcd。对于这道题的做法,每次把输入的两个数相乘在于前面的两个数gcd,如果gcd都等于1,就输出-1,否则从2到这个数的开方找能够整除的因数。

c++:

#include<bits/stdc++.h>
using namespace std;
long long divsor(long long n)
{
    for(int i=2; i*i<=n; i++)
        if(n%i==0) return i;
    return n;
}
int main()
{
    long long n,a,b,c,d;
    cin>>n>>a>>b;
    while(--n)
    {
        cin>>c>>d;
        a=__gcd(c*d,a),b=__gcd(c*d,b);
    }
    if (a!=1)cout<<divsor(a)<<endl;
    else if (b!=1) cout<<divsor(b)<<endl;
    else cout<<-1<<endl;
    return 0;
}

python:

from math import gcd,sqrt
def divsor(n):
    for i in range(2,int(sqrt(n))+1):
        if n%i==0:
            return i
    return n
n=int(input())
a,b=map(int,input().split())
for i in range(n-1):
    c,d=map(int,input().split())
    a=gcd(c*d,a);b=gcd(c*d,b)
if a!=1:
    print(divsor(a))
elif b!=1:
    print(divsor(b))
else:
    print(-1)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/81866192
今日推荐