思路:

首先选取任意一对数(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 ;
}

最新文章

  1. 设计模式--享元模式Flyweight(结构型)
  2. python网络编程——IO多路复用之epoll
  3. css 笔记
  4. 鸟哥的Linux私房菜第零章
  5. ChartDirector应用笔记(二)
  6. HtmlPrefixScopeExtensions
  7. 什么是HttpOnly
  8. poj 2723 2-SAT问题
  9. POJ 2249
  10. ASP.NET MVC轻教程 Step By Step 5——初识表单
  11. 使用HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
  12. PHP搭建简单暴力的mvc
  13. Atomic变量和Thread局部变量
  14. 16Khz音频定时触发采样DMA存储过程
  15. iOS代码组件化--利用cocoaPods创建私有库
  16. 归并排序python实现
  17. Unity3D笔记 GUI 二 、实现选项卡一窗口
  18. html自适应布局,@media screen,媒体查询
  19. Codeforces801D Volatile Kite 2017-04-19 00:30 122人阅读 评论(0) 收藏
  20. HTML的代码规范

热门文章

  1. Java连接redis操作数据
  2. Dubbo原理与框架设计
  3. C#视频取帧图
  4. vim vi Ubuntu 设置
  5. python 中main函数总结
  6. UVa 753 A Plug for UNIX (最大流)
  7. POJ - 3414 Pots BFS(著名倒水问题升级版)
  8. .NET Core 3.0之深入源码理解Configuration(三)
  9. ASP.NET Core Web API + Angular 仿B站(三)后台配置 JWT 的基于 token 的验证
  10. 如何实现Ant design表单组件封装?