思路:

首先选取任意一对数(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. 自己实现c++中string 类

    class String { public: String(const char *str = NULL);// 普通构造函数 String(const String &other);// 拷 ...

  2. java:calendar类及一些比较实用的utils(二)

    在这里将我在项目中用到的一些关于使用Calendar的utils分享出来,只是一部分,后期遇到好的通用方法会继续添加,以和大家交流学习,如果你还不熟悉这个类的使用,但是急需某个util,可以加群:41 ...

  3. [转载]Doxygen C++ 注释风格

    转载自:http://luchenqun.com/?p=761 做一个C++方面的符合Doxygen的注释文档,备用. 1.头文件根原文件注释.这个我也不知道需要注释什么.能想到的是:谁写的,里面有些 ...

  4. Broken robot

    题意: 要在$nm$的矩阵中从 $(i,j)$ 处移动到第 $n$ 行,每次移动可在不动.左移一格.右移一格.下移一格 4 种选择中等概率随机选一种,但移动不能超出矩阵.求移动次数的期望,最少保留4位 ...

  5. Spring boot实例

    代码下载http://pan.baidu.com/s/1c2aXLkc 密码:2joh 1.代码包规划 Application主类 package com.smart; import org.spri ...

  6. Asset Catalog Help (十)---Specifying a Resizable Area of an Image

    Specifying a Resizable Area of an Image Use the Xcode Slicing feature to specify the dimensions of a ...

  7. python 之队列

    进程和线程模块下都有队列类. 线程队列: # 后进先出->堆栈 q=queue.LifoQueue(3) # 优先级队列,数字越小优先级越高 q=queue.PriorityQueue(3) 进 ...

  8. jquery的on()绑定无效的方式

    on前面的元素也必须在页面加载的时候就存在于dom里面.动态的元素或者样式等,可以放在on的第二个参数里面 jQuery on()方法是官方推荐的绑定事件的一个方法.使用 on() 方法可以给将来动态 ...

  9. 洛谷 - P1582 - 倒水 - 位运算

    https://www.luogu.org/problemnew/show/P1582 要求用最少的瓶子,那肯定不能有两个一样的瓶子,否则合并更优. 枚举其二进制位,每次加上lowbit,将最后一个1 ...

  10. hdu3257【模拟】

    题意: 从案例找: 思路: 就是16进制,然后到2进制= =.就是个模拟= =.注意格式: #include <bits/stdc++.h> using namespace std; ty ...