Codeforces #505(div1+div2) B Weakened Common Divisor
题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1?
思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大枚举即可,需要特判一下第一个元素是素数的情况。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
#define INF 0x3f3f3f3f
#define pii pair<int,int>
#define LL long long
#define mk(a,b) make_pair(a,b)
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
LL a[200010][2];
inline LL gcd(LL x,LL y){
return y?gcd(y,x%y):x;
}
int main(){
int n;
scanf("%d",&n);
LL sum=0,ans=0;
for(int i=1;i<=n;i++){
scanf("%lld%lld",&a[i][0],&a[i][1]);
sum=gcd(a[i][0]*a[i][1],sum);
}
if(sum==1){
printf("-1\n");
return 0;
}
for(int i=2;i*i<=a[1][0];i++){
if(ans==0&&sum%i==0)ans=i;
while(a[1][0]%i==0)a[1][0]/=i;
}
if(ans==0&&a[1][0]>1&&sum%a[1][0]==0)ans=a[1][0];
for(int i=2;i*i<=a[1][1];i++){
if(ans==0&&sum%i==0)ans=i;
while(a[1][1]%i==0)a[1][1]/=i;
}
if(ans==0&&a[1][1]>1&&sum%a[1][1]==0)ans=a[1][1];
printf("%lld\n",ans);
}
Codeforces #505(div1+div2) B Weakened Common Divisor的更多相关文章
- Codeforces #505(div1+div2) D Recovering BST
题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...
- Codeforces #505(div1+div2) C Plasticine zebra
题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...
- codeforces#505--B Weakened Common Divisor
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- CF1025B Weakened Common Divisor 数学
Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor
[链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. ...
- CodeForces - 1025B Weakened Common Divisor
http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...
- CF #505 B Weakened Common Divisor(数论)题解
题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1. 思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘, ...
- codeforces 1025B Weakened Common Divisor(质因数分解)
题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...
- CF1025B Weakened Common Divisor【数论/GCD/思维】
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
随机推荐
- zoj 3965 Binary Tree Restoring(搜索)
Binary Tree Restoring Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Given two ...
- 总结一下内核DEBUG中的dump_stack, BUG, BUG_ON以及panic
有点空闲时间,让我们来总结一下内核DEBUG中的各个语句吧.随便找个内核驱动,在init函数里面加入如下代码测试: u8 a = 1, b = 0; printk("----------du ...
- 64位Navicat Premium安装/破解【含资源】
开门见山: 1/先安装Navicat Primium,双击Navicat Primium——trial_64.exe(64位) 2/安装好打开Navicat Primium,提示使用或注册时,双击Pa ...
- Struts2(1)
一.struts概述 1.struts2框架应用在javaee三层结构中web层框架 2.struts2框架在struts1和webwork基础之上发展全新的框架 二.struts2环境搭建 1.导包 ...
- Python 2.7_爬取妹子图网站单页测试图片_20170114
1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...
- python数据类型,int,str,bool
一,python中的int() int在python中主要用来运算,对字符串的转化,用int(str)表示,并且需要str.isdigit为真. 在int()中二进制的转换如下: #bit_lengt ...
- UFLDL新版教程
http://ufldl.stanford.edu/tutorial/ 还带编程作业.
- redis之 centos 6.7 下安装 redis-3.2.5
前期准备:1. 操作系统需要安装 gcc 包 与 TCL 库, 通过配置本地 yum 源 ,yum -y install gcc . yum -y install tcl安装2. 下载 redis ...
- 解决mac下sublime中文乱码
Mac OS X 属于 Apple 独家演绎的 Unix 分支版本,默认使用 UTF-8 编码,当使用不同开发平台的小伙伴们,共同维护一份代码的时候,尤其现在很多人都还在用 Windows 系统的时候 ...
- LOJ 10189 仓库建设 ——斜率优化dp
题目:https://loj.ac/problem/10189 #include<iostream> #include<cstdio> #include<cstring& ...