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 ...
随机推荐
- https证书自签
https http over ssl = https 443/tcp ssl: v3 tls: ...
- react之redux增加删除数字
比如在页面中添加和删除‘222’ action.js export const ADD= 'ADD'; export const RED='RED'; export const add=(str)=& ...
- SSH框架中hibernate 出现 user is not mapped 问题
SSH框架中hibernate 出现 user is not mapped 问题 在做SSH框架整合时,在进行DAO操作时.这里就只调用了chekUser()方法.运行时报 user is ...
- BZOJ2599:[IOI2011]Race
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- io_service work 的作用
当有任务的时候,run函数会一直阻塞:但当没有任务了,run函数会返回,所有异步操作终止. 客户端程序中,如果我想连接断开后重连,由于连接断开了,run会返回,当再次重连的时候,由于run返回了,即使 ...
- 如何给容器服务的Docker增加数据盘
如何给容器服务的Docker增加数据盘 摘要: 我们知道Docker的数据是通过联合文件系统的方式存储到磁盘上,当需要在机器上运行的容器或者镜像的数量不断增加时,有可能磁盘的大小不再满足需求,这个时候 ...
- python 中main函数总结
Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入时自动执行,这些代码,可以认为是Python的main函数. 每个文件(模块)都可以任意写一些没有缩进的代码 ...
- 2-2和2-3基本数据类型 & 2-4基本数据类型详解 & 3-1和3-2整形字面量值及变量声
2-4基本数据类型详解 3-1和3-2整形字面量值及变量声 023是八进制的 0x1357是十六进制 0X3C也是十六进制 0x1abL:长整型 变量声明 数据类型 空格 变量名 赋值: 变量的定义:
- 3-2if条件结构
不同条件做不同的操作.例如满100就减去20 条件结构 package com.imooc.operator; public class ConditionDemo1 { public static ...
- [教程心得] Flash AIR 调用exe/bat且可以传参
Flash AIR 如何调用exe/bat?并且有些情况下需要传参,如何传参呢? 看下面例子: cmd传参打开系统软键盘(参考http://bbs.9ria.com/thread-181265-1-1 ...