CF #505 B Weakened Common Divisor(数论)题解
题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1。
思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘,然后求每组的GCD,那么这个GCD就是因数的乘积(如果GCD==1就输出-1)。然后打个2e5的素筛表,然后找到GCD的一个素数因数。做到这里好像没问题了,然而,分分钟会被hack,问题出在哪里了?显然啊,打素数表只用2e5的范围,但是因数还包括自己本身啊!所以一旦GCD大于2e5我们就找不到答案了,那么我用一个set来储存遇到的大于2e5的数,如果素筛没找到,那就去set里遍历就行了。
思路2:还是那个问题,大于2e5的素数怎么解决。大佬告诉我一个方法,把第一组数据质因数分解,如果分解到最后发现第一组数据的质因数出现了大于2e5的素数,那么就把他存起来,在后面判断。
混合场很不友好,然而我并没有报名,帮别人掉分(逃
代码1:
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = 2e5+;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
int pr[maxn],p[maxn],pn;
set<ll> st;
void get(){
pn = ;
memset(pr,,sizeof(pr));
for(int i = ;i < maxn;i++){
if(!pr[i]){
p[pn++] = i;
for(ll j = (ll) i * i;j < maxn;j += i){
pr[j] = ;
}
}
}
}
ull gcd(ull a,ull b){
return b == ? a : gcd(b,a % b);
}
int main(){
int n;
get();
st.clear();
scanf("%d",&n);
ull u,v;
scanf("%lld%lld",&u,&v);
if(u >= maxn) st.insert(u);
if(v >= maxn) st.insert(v);
ull GCD = u*v;
bool flag = true;
for(int i = ;i <= n;i++){
scanf("%lld%lld",&u,&v);
if(u >= maxn) st.insert(u);
if(v >= maxn) st.insert(v);
GCD = gcd(GCD,u * v);
if(GCD == ){
flag = false;
break;
}
}
if(flag){
for(int i = ;i < pn && p[i] * p[i] <= GCD;i++){
if(GCD % p[i] == ){
printf("%d\n",p[i]);
return ;
}
}
for(set<ll>::iterator it = st.begin(); it != st.end();++it){
ll u = *it;
if(GCD % u == ){
printf("%lld\n",*it);
return ;
}
}
printf("%lld\n",GCD);
}
else printf("-1\n");
return ;
}
代码2:
/*
author :竹攸
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 2e5+;
typedef long long ll;
ll p[maxn],num[maxn],k,a[maxn],b[maxn]; void prime(){
for(int i = ; i < maxn; i++){
if(!num[i]){
for(ll j = 1LL*i*i; j < maxn; j+=i){
num[j] = ;
}
}
}
k = ;
for(int i = ; i < maxn;i++){
if(!num[i])
p[++k] = i;
}
} ll gcd(ll a,ll b){
return b == ?a:gcd(b, a%b);
} int main(){
int n;
ll x, y, a, b, sa, sb;
prime();
while(scanf("%d",&n)!=EOF){
scanf("%lld%lld",&x,&y);
num[] = x*y;
if(n == ){ //n等于1的时候先输出
printf("%lld\n",x);
continue;
}
for(int i = ; i <= k&& x >= p[i]; i++){ //判断x中是否存在大于2e5的素数
while(x % p[i] == ){
x /= p[i];
}
}
if(x == )
a = 2e9+;
else
a = x;
for(int i = ; i <= k&& y >= p[i]; i++){ //y与x 同理
while(y % p[i] == ){
y /= p[i];
}
}
if(y == )
b = 2e9+;
else
b = y;
sa = sb = ;
for(int i = ; i <= n; i++){
scanf("%lld%lld",&x,&y);
num[i] = x*y;
if(x % a == || y % a == )
sa++;
if(y % b == || x % b == )
sb++;
}
if(sa == n){
printf("%lld\n",a);
continue;
}
else if(sb == n){
printf("%lld\n",b);
continue;
}
ll Gcd = num[];
for(int i = ; i <= n; i++)
Gcd = gcd(num[i],Gcd);
int ans = ;
for(int i = ; i <= k;i++){
if(Gcd % p[i] == ){
ans = p[i];
break;
} }
if(ans == && Gcd != )
printf("%lld\n",Gcd);
else if(ans == && Gcd == )
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
CF #505 B Weakened Common Divisor(数论)题解的更多相关文章
- codeforces#505--B Weakened Common Divisor
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- 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 ...
- 【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 #505(div1+div2) B Weakened Common Divisor
题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...
- 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 - 1025B Weakened Common Divisor
http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...
- CF1025B Weakened Common Divisor
思路: 首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件. 时间复杂 ...
- codeforces 1025B Weakened Common Divisor(质因数分解)
题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...
随机推荐
- jQuery的一些小技巧()
1.判断某元素上是否绑定了某种类型的事件 var $events = $("#foo").data("events"); if( $events &&a ...
- Windows Phone 有关独立存储(一)
private const string foldername = "temp1"; private const string filename = foldername + &q ...
- mysql数据库LOAD DATA INFILE Syntax
1.LOAD DATA INFILE用来把一个文本文件里的内容高速写入到MySQL表里,它和SELECT ... INTO FILE的操作是对应的,一个导入.一个导出.使用LOAD DATA INFI ...
- (转)Python学习路径及练手项目合集
转载自知乎 Wayne Shi,仅仅为了方便收藏查看,侵权删. 阶段1:入门知识 零编程基础的可以先从下面几个教程了解编程及环境入门知识.(已有编程基础直接从阶段2起步) 1. 编程新手指南2. Li ...
- CodeForces 651 C Watchmen
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- 2018CCPC秦皇岛站
这次去秦皇岛,两个队都打铁回来,真的是蛮耻辱的 可以说是有史以来最差成绩了 其实网络赛就打的不好 两个名额一个是省赛分配的一个是排队排来的 去之前拉了几场之前的CCPC的比赛打 感觉打的都还不错的 热 ...
- centos7 iptables/firewalld docker open port
here are multiple "hackish" ways to do it: scan kernel logs, as mentioned by Jiri (but you ...
- Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)
RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...
- 大规模Docker平台自动化监控之路
本文介绍了通过Monitor,如何实现大规模容器运维平台的自动化监控需求. 尽管Docker技术目前还处于不稳定的发展与标准制定阶段,但这门技术已经呈现了极其火热的增长状态,却已经是不争的实事.到底有 ...
- saltstack相关
通过saltstack实现根据不同业务特性进行配置集中化管理,分发文件,采集服务器数据,操作系统基础及软件包管理等第一层为web交互层,采用django+mysql+bootstarp实现,服务端采用 ...