Codeforces Round #505 B. Weakened Common Diviso(思维题,求因子)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/81901131

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) , (a2,b2) , ..., (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)], 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≤150000) — the number of pairs.

Each of the next nn lines contains two integer values aiai , bibi (2≤ai,bi≤2⋅1e9 ).

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

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

题意:

给你n对数,让你找一个大于1的数,使得每一对数里面,至少有一个数能被n整除

解析:

这道题有一点卡时间..做的时候都被T了

两种做法

1.用第一对数a,b分别与下面n-1对的每一对的乘积做gcd,当前对为x,y,那么a=gcd(a,x*y),b=gcd(b,x*y)

然后在求a/b的>1的最小因子,如果a,b无法找到最小因子但,a/b>1,那么a/b就是质数的情况,只要输出这个质数就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
typedef long long ll;
using namespace std;

const int MAXN = 2e5+10;

ll a[MAXN][2];

ll gcd(ll a,ll b)
{
    while(b)
    {
        ll tmp=a%b;
        a=b;
        b=tmp;
    }
    return a;
}

int main()
{

	ll a, b,ck;
	int n;
	int flag=1;
	scanf("%d", &n);
	scanf("%lld%lld", &a, &b);
	if (n == 1)
    {
		printf("%lld\n", a);
		return 0;
	}
    ll x,y;
	for (int i = 1; i < n; i++)
    {
		scanf("%lld%lld", &x, &y);
		ll tmp=x*y;
		a = gcd(a, tmp);
		b = gcd(b, tmp);
	}

	if(a!=1||b!=1)
	{
		for (ll i = 2; i*i<=a||i*i<=b; i++)
		{
			if(a%i==0||b%i==0)
            {
                printf("%lld\n",i);
                return 0;
            }
		}
		if(a!=1) printf("%lld\n",a);
		else printf("%lld\n",b);
	}
	else printf("-1\n");
	return 0;
}

2.暴力做法,把第一队a,b的因子全部找出来,注意还要把他们自己本身也加进去,为了出现质数的情况

然后用每一个因子去遍历剩余的n-1对,找到一个符合条件的就输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
typedef long long ll;
using namespace std;

const int MAXN = 2e5+10;

ll a[MAXN][2];
ll p[MAXN];
int cnt;

void d(ll x)
{
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            p[cnt++]=i;
            while(x%i==0) x=x/i;
        }
    }
    if(x>1) p[cnt++]=x;
}

int main()
{

	int n;
	cnt=0;
	int flag=1;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
        scanf("%lld%lld", &a[i][0], &a[i][1]);
	d(a[1][0]);
	d(a[1][1]);
	int j;
	for(int i=0;i<cnt;i++)
    {
        for(j=2;j<=n;j++)
        {
            if(a[j][0]%p[i]!=0&&a[j][1]%p[i]!=0)
            {
                break;
            }
        }
        if(j==n+1)
        {
            printf("%lld\n",p[i]);
            return 0;
        }
    }
    printf("-1\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/81901131