题目描述:

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)(a1​,b1​) , (a_2, b_2)(a2​,b2​) , ..., (a_n, b_n)(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 , 55or 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.

(洛谷有中文题面)

算法标签:gcd

题目大意:

存在n对数,要求询问是否存在一个大于1的数使得这个数能被每一对数中的至少一个数整除。若不存在输出-1,否则输出任意一个满足条件的数。

思路:

对于每一对数计算两个数的lcm,再把每一对数得到的lcm取gcd,获得的数若为1则无解输出-1。否则必有解,因为我们要保证得到的数的所有因数必须仅属于一对数的其中一个数,所以我们获得的数的最小因数,即可保证其必然仅属于每一对数的其中一个。

以下代码:

#include<bits/stdc++.h>
#define il inline
#define LL long long
#define _(d) while(d(isdigit(ch=getchar())))
using namespace std;
int n;LL g;
il int read(){int x,f=1;char ch;_(!)ch=='-'?f=-1:f;x=ch^48;_()x=(x<<1)+(x<<3)+(ch^48);return f*x;
}
il LL gcd(LL x,LL y){return x==0?y:gcd(y%x,x);
}
il int cal(int x){for(int i=2;i*i<=x;i++){if(x%i==0)return i;}return x;
}
int main()
{n=read()-1;int a=read(),b=read();g=1ll*a*b/gcd(a,b);while(n--){a=read();b=read();g=gcd(g,1ll*a*b/gcd(a,b));}if(g==1)puts("-1");else{int k=gcd(g,a);if(k>1)printf("%d\n",cal(k));else{k=gcd(g,b);printf("%d\n",cal(k));}}return 0;
}

View Code

转载于:https://www.cnblogs.com/Jessie-/p/10362326.html

CF1025B Weakened Common Divisor相关推荐

  1. 【CF1025B】 Weakened Common Divisor

    题目 题目描述 与 GCDGCD (最大公约数)类似,我们引进 WCDWCD (弱公约数)的概念, WCDWCD 的定义如下: 给出几对数 \left( a_1,b_1 \right) ,\left( ...

  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. 最大公约数(Greatest Common Divisor)【算法及实现】

    概述 最大公约数(Greatest Common Divisor),也称最大公因数.最大公因子.是指两个或多个整数公有因数中最大的一种.编程时常用 gcd(a, b) 表示两个数的最大公约数. 相反的 ...

  9. Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes.

    用埃拉托色尼筛选法计算两个整数的最大公约数 最近在回顾算法,会相继贴一些自己写的代码,希望在分享的同时,能够得到观看者的指教,以求共同进步. 以下为我写的程序,运行环境:Dev-C++ 5.4.0. ...

最新文章

  1. PostgreSQL 8.2.5 安装为 Windows 服务 (Service)
  2. ubuntu没有interfaces文件_ubuntu里终端中eth0为何没有显示IP地址
  3. CCNP实验【静态出接口配置】
  4. git 简单操作流程图
  5. 解决 mysql>com.mysql.jdbc.PacketTooBigException: Packet for query is too large (12073681 > 4194304)
  6. 离京2小时,快手百亿入云,一切为了「看见」
  7. 判别模型、生成模型和朴素贝叶斯模型
  8. php mysql 命令行模式_PHP 的命令行模式
  9. 作为Scala语法糖的设计模式
  10. 高德地图拾取经纬度 + 搜索 + 标记
  11. php ddos防御,网站防御DDOS的PHP代码
  12. ROS:机器人系统设计(连接摄像头、Kinect、激光雷达、URDF建模)
  13. 手机网络 G、E、H、H+、4G 都是什么意思
  14. QT随手记:解决opencv播放USB视频延迟、拖影的方法
  15. 艾滋hiv最新研究进展(2022年4月)
  16. 思想,坚持,信仰,一切
  17. 2021年全国在映影院数量、观影人次及平均票价走势分析[图]
  18. 【分区助手】如何扩大C盘容量?
  19. 什么是FD.IO/VPP?
  20. kalilinux链接蓝牙音响_Kali Linux安装驱动并使用Blueman连接蓝牙耳机

热门文章

  1. 人一旦选择活下去,就必须为了生活起早贪黑,四处奔波
  2. ssh项目(包裹柜):换了另一台服务器上的数据库之后与数据库建立不了连接
  3. Java面试题-day03面向对象
  4. Linux显示行号设置
  5. 【莫烦Python】Matplotlib Python 画图教程 figure图像
  6. 一文讲透植物内生菌研究怎么做 | 微生物专题
  7. java环境变量配置:“javac不是内部或外部命令,也不是可运行的程序”解决
  8. 服务器把高清图片显示成表情图,图片上传被压缩怎么办?教你如何让公号图片变得高清!_【如何,得高,怎么,图片,表情包CC】...
  9. adb将Apk内置到系统中(system/priv-app)
  10. 使用Python播放mp3格式音频