题目

题目描述
与 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");
}

【CF1025B】 Weakened Common Divisor相关推荐

  1. CF1025B Weakened Common Divisor

    题目描述: During the research on properties of the greatest common divisor (GCD) of a set of numbers, Il ...

  2. Codeforces Round #505 B Weakened Common Divisor (cf 1025B)

    题目:Weakened Common Divisor 题意: 比赛时我的思路是求出每一对ai和bi的lcm,计作ci,再求出gcd(ci),最后求出这个gcd的一个质因数即可.问题是质因数的分解问题, ...

  3. CF #505 B Weakened Common Divisor

    CF #505 B Weakened Common Divisor 题意:问是否存在一个数,每组数据中都有数据可以整除它,并且这个数要大于1,如果存在多组数据随便输出一个,如果不存在,则输出-1. 这 ...

  4. cf----2019-10-03(Minimum Value Rectangle,Plasticine zebra,Weakened Common Divisor)

    城市黎明的灯火,总有光环在陨落,模仿者一个又一个,无人问津的角色,你选择去崇拜谁呢,怨恨谁呢? You have nn sticks of the given lengths. Your task i ...

  5. Codeforces Round #505 B. Weakened Common Divisor(思维)

    题目链接:http://codeforces.com/contest/1025/problem/B        题意是给了n组数,从每组数里挑一个数出来,求他们的因子,如果没有因子(也就是因子为1) ...

  6. CodeForces - 1025B Weakened Common Divisor

    http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...

  7. CodeForces-1205B Weakened Common Divisor

    先找出最小的一对数字,把他们所有的因数找出来,保存在数组里 接下来过一遍数字对,每次都把能够整除至少一个数字的因数保留,最后剩余的都可以作为答案 如果因数特别多会超时,所以在开始时尽量对因数分解,分解 ...

  8. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  9. C# .net Framework Windows窗体应用【02】

    本文代码仓库地址:gitee码云CSDN笔记仓库地址 目录 数据库 一.资源管理器的内容 二.项目 WindowsFormsApp_xiaoyin01 下的文件内容 1.Common 下的 FormF ...

最新文章

  1. Day9-Postfix
  2. Fortify:五大SOA架构都有安全漏洞
  3. 大厂首发!java敏捷开发模式面试题
  4. 在线场景感知:图像稀疏表示-ScSPM和LLC总结(lasso族、岭回归)
  5. 更改密码 sp_password
  6. 如何在Python中获取当前时间
  7. Hash (散列,哈希)
  8. Web前端工作笔记004---js--webSocket简单介绍和使用方法
  9. gpio stm8 管脚 配置工具_STM8S 外设模块的GPIO引脚应该如何配置
  10. 魔方机器人之上位机编程-------- 最无厘头错误(空格)
  11. BZOJ2728 HNOI2012与非(并查集+数位dp)
  12. 【4】Git查看版本库当前状态
  13. 各代iphone ipad iPod各种信息 获取设备型号等等整理
  14. 英文字母间隔突然增大(全角与半角转换)
  15. 离开华为三年,我才真正认同狼性文化
  16. BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Mode
  17. int型和char型之间的类型转换
  18. 20190919题目总结——选择题
  19. PostgreSQL数据库统计信息——examine_attribute单列预分析
  20. Hadoop安全之Kerberos

热门文章

  1. 普洛菲斯触摸屏4.08软件安装
  2. JDK8JDK17(windows-x64) 下载及环境配置
  3. oracle crs 4535,11gRAC报错CRS-4535,CRS-4000解决
  4. 【shell脚本】——归档文件脚本
  5. 表情包在线生成PHP源码
  6. VScode配置Leetcode环境
  7. linux磁盘分区表 清理,linux 磁盘分区表
  8. RTOS系统CPU使用率和任务堆栈空间统计方法
  9. python科学计算试题及答案_高校邦Python科学计算章节答案
  10. 最好的正规的有牌照的无卡支付app--沃银钱包