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 ...
随机推荐
- Maven常用操作
1. 修改Maven的本地仓库路径 1.1 默认会放在~/.m2/repository目录下 (“~”代表用户的目录,比如windows下一般都是C:\Documents and Settings\[ ...
- struts2漏洞原理
一.struts2简介: 目前web框架中非常流行的都是mvc设计模式.经典例子例如:python的Django.Flask:java的ssm等.因为使用MVC设计模式,所以在框架内部处理用户数据流参 ...
- 【Android】 ImageView.ScaleType设置图解
ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...
- Mac/Xcode - 开发技巧快捷键
Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...
- gradle下的第一个SpringMVC应用
新建gradle project 缺少了很多文件夹和文件,我们自己补充,补充完的目录如下: HelloController: package controller; import javax.serv ...
- 170803、springboot jar包启动提示没有主清单属性
问题:SpringBoot打包成jar后运行提示没有主清单属性 解决: 补全maven中的bulid信息 <plugin> <groupId>org.springframewo ...
- 2.06StuModify.aspx(修改姓名,性别,所在班级)
meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title ...
- Visual Studio 2017正式版离线安装方法
Visual Studio 2017 RTM正式版离线安装及介绍. 首先至官网下载:https://www.visualstudio.com/zh-hans/downloads/ VS 2017 正式 ...
- Python使用logging来记录日志
#encoding:utf-8 import logging.config from logging.handlers import RotatingFileHandler import Config ...
- Ubuntu18.04下安装比特币客户端
一.下载有两种安装方式:安装包和源码 二.安装1.通过安装包安装在https://bitcoin.org/en/download下载Windows,Mac OSX,Linux对应的安装包.安装过程比较 ...