题目
题目描述
与 GCDGCD (最大公约数)类似,我们引进 WCDWCD (弱公约数)的概念, WCDWCD 的定义如下:
给出几对数 \left( a_1,b_1 \right) ,\left( a_2,b_2 \right) ,\cdots ,\left( a_n,b_n \right)(a
1
,b
1
),(a
2
,b
2
),⋯,(a
n
,b
n
) ,它们的 WCDWCD 满足大于 1 1 ,且能整除每个数对中至少一个数。 WCDWCD 在一些情况下,可能不存在。
例如,给出这几对数 \left[ \left( \text{12,}15 \right) ,\left( \text{25,}18 \right) ,\left( \text{10,}24 \right) \right][(12,15),(25,18),(10,24)] ,它们的 WCDWCD 可以是 2,3,5,62,3,5,6 (这些数都满足严格大于 11 ,且能整除每个数对中至少一个数)
现在给你几对数,求他们的 WCDWCD 。
输入输出格式
输入格式
第一行一个整数 nn , 1\leqslant n\leqslant \text{150\ 000}1⩽n⩽150 000 表示数对的组数。
接下来 nn 行,每行两个整数 2\leqslant a_i,b_i\leqslant 2\cdot 10^92⩽a
i
,b
i
⩽2⋅10
9
输出格式
一个整数,表示这些数对的 WCDWCD ,如果这些数对不存在 WCDWCD ,输出 −1−1−1 。
样例解释
第一组样例中, 66 可以分别整除 18,24,1218,24,12 ,当然输出其他可能的答案也行。
第二个样例,没有满足条件的 WCDWCD 。
第三个样例,可以是 55 ,当然也可以是 3,153,15 等。你没有必要输出最大或者最小的答案。
感谢@enor2017 提供的翻译
题目描述
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 (a_1, b_1) (a
1
,b
1
) , (a_2, b_2) (a
2
,b
2
) , …, (a_n, b_n) (a
n
,b
n
) their WCD is arbitrary integer greater than 1 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)] [(12,15),(25,18),(10,24)] , then their WCD can be equal to 2 2 , 3 3 , 5 5 or 6 6 (each of these numbers is strictly greater than 1 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.
输入输出格式
输入格式:
The first line contains a single integer n n ( 1 \le n \le 150,000 1≤n≤150000 ) — the number of pairs.
Each of the next n n lines contains two integer values a_i a
i
, b_i b
i
( 2 \le a_i, b_i \le 2 \cdot 10^9 2≤a
i
,b
i
≤2⋅10
9
).
输出格式:
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 .
输入输出样例
输入样例#1: 复制
3
17 18
15 24
12 15
输出样例#1: 复制
6
输入样例#2: 复制
2
10 16
7 17
输出样例#2: 复制
-1
输入样例#3: 复制
5
90 108
45 105
75 40
165 175
33 30
输出样例#3: 复制
5
说明
In the first example the answer is 6 6 since it divides 18 18 from the first pair, 24 24 from the second and 12 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 1 satisfying the conditions.
In the third example one of the possible answers is 5 5 . Note that, for example, 15 15 is also allowed, but it’s not necessary to maximize the output.
思路
对于每一对数求它们的lcm,然后对于所有的lcm求gcd,显然gcd中的所有质因子每一对一定会出现,然后随便求一个质因子就好了。
代码
#include<cstdio>
inline int read(){
int a=0;char c=getchar();
for(;c<48||c>57;c=getchar());for(;c>47&&c<58;a=a*10+c-48,c=getchar());
return a;
}
int n,a,b,p[20],fl[20],t[20],k;
int main(){
int f;
n=read();
a=read();b=read();
for(int i=2; i*i<=a; i++){
f=0;
for(;a%i==0;)a/=i,f=1;
if(f)p[++k]=i;
}if(a>1)p[++k]=a;
for(int i=2; i*i<=b; i++){
f=0;
for(;b%i==0;)b/=i,f=1;
if(f)p[++k]=i;
}if(b>1)p[++k]=b;
for(;--n;){
a=read();b=read();
for(int j=1;j<=k;j++)
if(a%p[j]&&b%p[j])fl[j]=1;
}
for(int i=1; i<=k; i++) if(!fl[i]) return 0*printf("%d",p[i]);
printf("-1");
}
本文介绍了弱公约数(WCD)的概念,一种类似最大公约数但适用于数对集合的数学概念,并提供了一种高效计算方法。通过实例展示了如何确定一组数对的WCD,以及在不存在WCD情况下的处理方式。

257

被折叠的 条评论
为什么被折叠?



