CF1025B Weakened Common Divisor
2024-08-30 11:10:43
思路:
首先选取任意一对数(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 ;
}
最新文章
- 设计模式--享元模式Flyweight(结构型)
- python网络编程——IO多路复用之epoll
- css 笔记
- 鸟哥的Linux私房菜第零章
- ChartDirector应用笔记(二)
- HtmlPrefixScopeExtensions
- 什么是HttpOnly
- poj 2723 2-SAT问题
- POJ 2249
- ASP.NET MVC轻教程 Step By Step 5——初识表单
- 使用HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
- PHP搭建简单暴力的mvc
- Atomic变量和Thread局部变量
- 16Khz音频定时触发采样DMA存储过程
- iOS代码组件化--利用cocoaPods创建私有库
- 归并排序python实现
- Unity3D笔记 GUI 二 、实现选项卡一窗口
- html自适应布局,@media screen,媒体查询
- Codeforces801D Volatile Kite 2017-04-19 00:30 122人阅读 评论(0) 收藏
- HTML的代码规范
热门文章
- Java连接redis操作数据
- Dubbo原理与框架设计
- C#视频取帧图
- vim vi Ubuntu 设置
- python 中main函数总结
- UVa 753 A Plug for UNIX (最大流)
- POJ - 3414 Pots BFS(著名倒水问题升级版)
- .NET Core 3.0之深入源码理解Configuration(三)
- ASP.NET Core Web API + Angular 仿B站(三)后台配置 JWT 的基于 token 的验证
- 如何实现Ant design表单组件封装?