题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的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的更多相关文章

  1. Codeforces #505(div1+div2) D Recovering BST

    题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...

  2. Codeforces #505(div1+div2) C Plasticine zebra

    题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...

  3. codeforces#505--B Weakened Common Divisor

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  4. CF1025B Weakened Common Divisor 数学

    Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  5. 【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] [题解] 求出第一个数对的两个数他们有哪些质因子. ...

  6. CodeForces - 1025B Weakened Common Divisor

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

  7. CF #505 B Weakened Common Divisor(数论)题解

    题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1. 思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘, ...

  8. codeforces 1025B Weakened Common Divisor(质因数分解)

    题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...

  9. CF1025B Weakened Common Divisor【数论/GCD/思维】

    #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...

随机推荐

  1. 吐槽XE3中的BUG:无法调试32位的应用程序

    我想用的功能在XE5中有BUG, 无奈转移到XE3中测试,发现了XE3还有另外一个问题:无法DEBUG 32位的应用程序,这算什么事啊?有人说把项目属性中的link with dynamic RTL去 ...

  2. 使用Innobackupex快速搭建(修复)MySQL主从架构

    MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一.但对于较大的数据库则该方式并非理想的选择.使用Xtrabackup可以快速轻松的构建或修复mysql主从架构.本文 ...

  3. angular复选框式js树形菜单(一)

    treeView.html <ul class="tree-view"> <li ng-repeat="item in treeData" n ...

  4. 455. Assign Cookies Add to List

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  5. Scala 面向接口编程

    1.如果要实现一个接口,前边没有extends关键字就可以使用extends,如果有要使用with关键字 2.Scala 中的接口支持多种继承,类或者抽象类不支持多种继承 3.抽象属性:未被实例化的属 ...

  6. JvisualVm添加远程监控

    一.Weblogic远程监控 1.首先需要在远程的weblogic的域下面,找到/bin/ setDomainEnv.sh ,需要在此文件下加入如下内容: -Dcom.sun.management.j ...

  7. BZOJ3624:[APIO2008]免费道路

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  8. flask之python3 虚拟环境及使用dotnv来永久保存环境变量

    Python 3 comes bundled with the venv module to create virtual environments Create an environment Cre ...

  9. 用Azure上Cognitive Service的Face API识别人脸

    Azure在China已经发布了Cognitive Service,包括人脸识别.计算机视觉识别和情绪识别等服务. 本文将介绍如何用Face API识别本地或URL的人脸. 一 创建Cognitive ...

  10. Azure SLB + httpd + ILB + HAProxy + Atlas + MySQL

    为了测试一个环境,需要在Azure上搭建高可用的LAMP架构.但要求MySQL的中间件Atlas采用主备的模式.在数据中心一般采用Keepalive+VIP的模式,通过浮动地址对外提供服务. 但在云环 ...