Codeforces 1025B Weakened Common Divisor

版权声明:转载请说明出处 https://blog.csdn.net/bao___zi/article/details/81866144

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), (a2,b2), ..., (an,bn) their WCD is arbitrary integer greater than 1, 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)], then their WCD can be equal to 2, 3, 5 or 6 (each of these numbers is strictly greater than 1 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 n (1≤n≤150000) — the number of pairs.Each of the next n lines contains two integer values ai, bi (2≤ai,bi≤2⋅10^9).

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.

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 6 since it divides 18 from the first pair, 24 from the second and 12 from the third ones. Note that other valid answers will also be accepted.

In the second example there are no integers greater than 1 satisfying the conditions.

In the third example one of the possible answers is 5 . Note that, for example, 15 is also allowed, but it's not necessary to maximize the output.

题意:给你n组a,b求一个wcd使得至少能整除每一组里的其中一个元素,并且wcd大于1,如果存在多个就输出其中一个wcd,没有就输出-1.

解题思路:我们先把第一组的a b求解质因数,然后对每一组的a,b 求一个最小公倍数。然后再对每组共同求一个最大公因数

那么求出来的最大公因数就包括了全部组的公因数,但是这不是我们需要的解。因为存在一种情况就是最大公因数里是由两个最大的素数组成。

那么你正常素数筛,然后求这个最大公因数的其中一个公因数肯定是不行的。

所以我们只需要一开始的时候求解其中一组的质因数就行。而且题目也不需要你求一个最大的公因数。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
using namespace std;
const int maxn=5e5+10;
const int inf=0x3f3f3f3f;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
ll n,m,p[maxn];
set<int>prime;
ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
void find(ll x){
	for(ll i=2;i*i<=x;i++){
		if(x%i==0){
			prime.insert(i);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) prime.insert(x);
}
int main(){
	int i,j;
	prime.clear();
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		ll x,y,te;
		scanf("%I64d %I64d",&x,&y);
		if(i==1){
			find(x);find(y);
		}
		te=gcd(x,y);
		p[i]=x*y/te;
	}
	ll t=p[1];
	for(ll i=2;i<=n;i++){
		t=gcd(t,p[i]);
	}
	set<int> ::iterator it;
	for(it=prime.begin();it!=prime.end();it++){
		if(t%(*it)==0){
			t=*it;break;
		}
	}
	if(t==1) printf("-1\n");
	else{
		printf("%lld\n",t);
	}
	return 0;
}
//2
//1999999973 1999999943
//1999999973 1999999943
//3999999832000001539  

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/81866144
今日推荐