思路:

首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件。

时间复杂度:O(sqrt(amax) + n * log(amax))。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
int a[MAXN], b[MAXN];
set<int> factor(int x)
{
set<int> res;
for (int i = ; i * i <= x; i++)
{
if (x % i == )
{
res.insert(i);
while (x % i == ) x /= i;
}
}
if (x != ) res.insert(x);
return res;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++) scanf("%d %d", &a[i], &b[i]);
set<int>s1 = factor(a[]);
set<int>s2 = factor(b[]);
set<int> st;
for (auto it: s1) st.insert(it);
for (auto it: s2) st.insert(it);
for (int i = ; i < n; i++)
{
set<int> tmp;
for (auto it: st)
{
if (it > a[i] && it > b[i]) continue;
else if (a[i] % it && b[i] % it) continue;
tmp.insert(it);
}
st = tmp;
}
if (st.empty()) puts("-1");
else printf("%d\n", *st.begin());
}
return ;
}

CF1025B Weakened Common Divisor的更多相关文章

  1. CF1025B Weakened Common Divisor 数学

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

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

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

  3. CF1025B Weakened Common Divisor 题解

    Content 定义 \(n\) 个数对 \((a_1,b_1),(a_2,b_2),(a_3,b_3),...,(a_n,b_n)\) 的 \(\text{WCD}\) 为能够整除每个数对中至少一个 ...

  4. codeforces#505--B Weakened Common Divisor

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

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

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

  6. CodeForces - 1025B Weakened Common Divisor

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

  7. Codeforces #505(div1+div2) B Weakened Common Divisor

    题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...

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

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

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

随机推荐

  1. php获取YouTube视频信息的方法

    YouTube的视频地址格式https://www.youtube.com/watch?v=[VIDEO_ID]例子:https://www.youtube.com/watch?v=psvkyf3Pz ...

  2. HDU2087(KMP入门题)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. 升级后,使用dynamic报错

    程序升级为.NET 4.0后,在程序中使用dynamic时出现下列错误 错误 5 预定义的类型“Microsoft.CSharp.RuntimeBinder.Binder”未定义或未导入错误 4 找不 ...

  4. saltstack api安装使用

    Salt自然也是提供api的,使用api对自动化有极大的帮助,我们使用rest风格的api,当然大家都知道salt是python写的,那么自然也就提供了对应的api,但是并不建议使用,因为调用pyth ...

  5. bzoj4430

    bit+容斥原理 我不会cdq分治只能用这个做法 考虑什么情况下不满足,至少有一个顺序不对就不行了,那么不满足的总有两对属性形成逆序对,那么我们用总方案数*2=n*(n-1)减去不符合的*2再/2就是 ...

  6. CS231n 2016 通关 第四章-NN 作业

    cell 1 显示设置初始化 # A bit of setup import numpy as np import matplotlib.pyplot as plt from cs231n.class ...

  7. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  8. web面试常见问题补充

    jquery Ajax $ajax({ Url:”test.html”,-----发送请求的地址 Async:true;------异步操作 Cache:true,-----可以从缓冲中加载 Type ...

  9. 3-1Java程序的执行流程

    3-1Java程序的执行流程 用记事本写一个简单的程序 存到:E:\java路径下 class HelloImooc{    public static void main(String[] agrg ...

  10. 《剑指offer》面试题1:为类CMyString添加赋值运算符函数——C++拷贝构造函数与赋值函数

    题中已给出CMyString的类定义,要求写赋值运算符函数. #include<iostream> #include<cstring> using namespace std; ...