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