CF1025B Weakened Common Divisor
思路:
首先选取任意一对数(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的更多相关文章
- CF1025B Weakened Common Divisor 数学
Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...
- CF1025B Weakened Common Divisor【数论/GCD/思维】
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
- CF1025B Weakened Common Divisor 题解
Content 定义 \(n\) 个数对 \((a_1,b_1),(a_2,b_2),(a_3,b_3),...,(a_n,b_n)\) 的 \(\text{WCD}\) 为能够整除每个数对中至少一个 ...
- codeforces#505--B Weakened Common Divisor
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- CF #505 B Weakened Common Divisor(数论)题解
题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1. 思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘, ...
- CodeForces - 1025B Weakened Common Divisor
http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...
- Codeforces #505(div1+div2) B Weakened Common Divisor
题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...
- 【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] [题解] 求出第一个数对的两个数他们有哪些质因子. ...
- codeforces 1025B Weakened Common Divisor(质因数分解)
题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...
随机推荐
- c和c++字符串分割
1.c++版本,第一个参数为待分割的字符串 , 第二个参数为分割字符串 std::vector<std::string> split(const std::string& s, c ...
- ABP 框架启程 及 ABP 翻译目录及传送门
准备动手写一套电商的系统,辗转收集了不少相关的开源项目,最后决定使用ABP作为起点. 在园子里好多人都在推广ABP.有个园友做了一个集合贴,方便大家使用 ABP集合贴 建议大家优先看 HK Zhan ...
- vue watch 深度监听以及立即监听
vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...
- 在windows64位的系统上面操作操作excel程序出现异常
1.把iis的网站程序引用池启用32位设置 2.把iis的网站程序引用池启用中标识修改为LocalSystem 2.代开cmd 输入mmc -32 添加组件服务在dcom组件中找到excel安全里添加 ...
- CodeForces - 377A Maze BFS逆思维
Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...
- selenium浏览器驱动下载地址整理
今天把手头有的一些关于selenium测试的资源整理了一下,分享出来. 1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是 ...
- MFC的CString使用
CString没有基类.一个CString对象由可变长度的一队字符组成.CString使用类似于Basic的语法提供函数和操作符.连接和比较操作符以及简化的内存管理使CString对象比普通字符串数组 ...
- 算法学习--Day10
今天开始了新一章的学习,前面的题目虽然做了几道,但是我觉得训练量仍然太小了.不过机试确实很多题目,并且难度也有所不同,所以要针对不同的题目进行专门的练习才好.题目类型有些多,等接下来我将搜索的题目写完 ...
- Lightoj1012【DFS】
题意: 输出和' @ '相连有多少个' . '包括' @ ',' # '代表墙不能走: 思路: 基础DFS,找到起点,然后跑一下DFS就好了: #include<cstdio> #incl ...
- Matplotlib 如何显示中文
Python 3.x 主要是如下代码 import os font = FontProperties(fname=os.path.expandvars(r"%windir%\fonts\si ...