CF1025B Weakened Common Divisor【数论/GCD/思维】
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define mod 998244353
#define pi acos(-1.0)
#define rep(i,x,n) for(int i=(x); i<(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 1<<30;
const int maxn = 150000+3;
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[2]={-1,1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int t,n,m,d;
int cnt=0;
LL lcm(LL a, LL b)
{
return a/__gcd(a,b)*b;
}
LL cal(LL n)
{
for(int i=2;i*i<=n;i++)
if(n%i==0) return i;
return n;
}
LL a,b,x,y;
int main()
{
cin>>n>>a>>b;
for(int i=1;i<n;i++)
{
cin>>x>>y;
a=__gcd(x*y,a);
b=__gcd(x*y,b);
}
if(a!=1)
printf("%lld\n",cal(a));
else if(b!=1)
printf("%lld\n",cal(b));
else puts("-1");
}
/*
2
3 1
1 1 2
3 2
1 1 2
【题意】
给定n对数,求一个WCD,它满足至少能被每对数中的一个整除,若不存在,输出-1。
【类型】数论
【分析】一开始的思路是求每对数的最小公倍数,然后把这n个最小公倍数求个gcd,然后取其最小因子即可。但这样因为TLE而FST了。后来想想也是,如果每对数中的两个数互质,那么他们的最小公倍数就是1e18左右的大小,求其最小因子的时间复杂度差不多就是1e9,肯定会T。比如下面这组样例:
2
1999999973 1999999943
1999999973 1999999943
其实正解想法差不多,就把第一对中的第一个数和后面每对的乘积求一个gcd,第二个数也和后面的每对的乘积求一个gcd,这样就保证这两个数都是小于等于2e9的,求其最小因子的复杂度<1e5,可行。
PS:其实并不需要求每对数的最小公倍数,求其乘积即可,因为乘积包括了每对数那2个数中的所有因子,且乘积的最小因子一定能被每对数那2个数中的1个整除。
【时间复杂度&&优化】
【trick】
【数据】
CF1025B Weakened Common Divisor【数论/GCD/思维】的更多相关文章
- 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
思路: 首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件. 时间复杂 ...
- 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 ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- 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 1025B Weakened Common Divisor(质因数分解)
题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...
随机推荐
- Http字段含义
转载自:http://blog.csdn.net/sand_ant/article/details/10503579 一.request请求Header简介 Accept:--客户机支持的类型 Acc ...
- LightOJ 1013 - Love Calculator LCS
题意:找一个串使给出的两个串都是它的子串,要求最短,求出最短长度,以及种类数. 思路:可以想到,当两个子串a,b拥有最长的公共子串为LCS时,那么可以求出的最短的串为lena+lenb-LCS. 那么 ...
- jsp 内置对象二
1.什么是session ? (1)session 表示客户端与服务器的一次回话. 2)Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网 ...
- shell 25个常用命令
1.列出所有目录使用量,并按大小排序. ls|xargs du -h|sort -rn #不递归下级目录使用du -sh 2.查看文件排除以#开关和空白行,适合查看配置文件. egrep -v ...
- git设置免密码登录
设置用户名和邮箱 git config --global user.name "<username>" git config --global user.email & ...
- 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...
- word 宏,脚本编程
'脚本方式新建word 再新建文档,文档中输入字符串"你好" Dim wdapp As Word.Application Dim wddoc As Word.Document Se ...
- 小程序_RSA加密功能
这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...
- $.on方法与$.click()的区别
1.$.on("click") 支持动态元素绑定事件,该事件是绑定到document上,只要符合条件的元素即可绑定事件,同时$.on()可以绑定多个事件 on方法 on(event ...
- C语言restrict限定符
restrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式.即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其 ...