【CF 1025B】Weakened Common Divisor

                             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組數字,找出一個數,使得所有組兩個數字中至少有一個是它的倍數。

注意!答案不唯一,例如樣例一可以輸出2,3,6;而樣例三可以輸出3,5,15;只需要輸出其中一個即可。

思路:對第一組數據進行處理,找出其所有質因數,然後用後面的元素除以這些質因數,找出第一個被所有組數據整除的質因數。

代碼:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))

int a[150005],b[150005];
set<int>q;

void prm(int n)
{
	int i;
	for(i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			q.insert(i);
			while(n%i==0)
			n/=i;
		}
	}
	if(n!=1)             //如果n是質數,將其插入set數組
		q.insert(n);
}

int main()
{
	int n,i,flag;
	closeio;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i]>>b[i];
	prm(a[0]);
	prm(b[0]);
	set<int>::iterator it;
	//for(it=q.begin();it!=q.end();it++)
	//cout<<*it<<endl;
	for(it=q.begin();it!=q.end();it++)
	{
		flag=1;
		int t=*it;
		for(i=1;i<n;i++)
			if(a[i]%t!=0&&b[i]%t!=0)
			{
				flag=0;
				break;
			}
		if(flag)
		{
			cout<<t<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章