B. Weakened Common Divisor

http://codeforces.com/contest/1025/problem/B

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.

题意:

保证从每组数中取出一个数,使所有的数的公因数大于1

分析:

找出第一个数的质因子就行

(暴力,按理说不能过,但是过了)

暴力代码:

#include<bits/stdc++.h>
using namespace std;
int GCD(int a,int b){
return b == 0 ? a : GCD(b,a%b);
}
int ans,n,aa[151000],bb[151000];
void dfs(int len,int num)
{
    if(num==1)return;
    if(ans!=-1)return;
    if(n==len)
    {
        ans=num;
        return;
    }
    int  tt,ss;
    ss=GCD(num,aa[len]);
    tt=GCD(num,bb[len]);
    if(GCD(ss,tt)==tt)
    dfs(len+1,ss);
    else
    if(GCD(ss,tt)==ss)
    dfs(len+1,tt);
    else
    {
        dfs(len+1,ss);
        dfs(len+1,tt);
    }
}
int main()
{
    int i,j;
    scanf("%d",&n);
    ans=-1;
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&aa[i],&bb[i]);
    }
    dfs(1,aa[0]);
    dfs(1,bb[0]);
    printf("%d\n",ans);
}

真代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5e5+100;
const int N=1e7;
const int INF=1e9+7;
const int mod=1e9+7;
int a[maxn];
int b[maxn],c[maxn];
int ans=0;

void go(int x)
{
    int i;
    for(i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            c[ans++]=i;
            while(x%i==0)x/=i;
        }
    }
    if(x>1)c[ans++]=x;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    go(a[1]);
    go(b[1]);
   // c[ans++]=a[1];
    //c[ans++]=b[1];
    sort(c,c+ans);
    ans=unique(c,c+ans)-c;
    int flag=0;
    int id=0;
    for(int i=0;i<ans;i++)
    {
        int sum=0;
        for(int j=2;j<=n;j++)
        {
            if(a[j]%c[i]==0||b[j]%c[i]==0)
            {
                sum++;
            }
        }
        if(sum==n-1)
        {
            flag=1;
            id=c[i];
            break;
        }
    }
    if(flag)
    {
        printf("%d\n",id);
    }
    else printf("-1\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81878180