题意:给你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(数论)题解的更多相关文章

  1. codeforces#505--B Weakened Common Divisor

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  2. CF1025B Weakened Common Divisor 数学

    Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  3. CF1025B Weakened Common Divisor【数论/GCD/思维】

    #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...

  4. 【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] [题解] 求出第一个数对的两个数他们有哪些质因子. ...

  5. Codeforces #505(div1+div2) B Weakened Common Divisor

    题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...

  6. CF1025B Weakened Common Divisor 题解

    Content 定义 \(n\) 个数对 \((a_1,b_1),(a_2,b_2),(a_3,b_3),...,(a_n,b_n)\) 的 \(\text{WCD}\) 为能够整除每个数对中至少一个 ...

  7. CodeForces - 1025B Weakened Common Divisor

    http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...

  8. CF1025B Weakened Common Divisor

    思路: 首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件. 时间复杂 ...

  9. codeforces 1025B Weakened Common Divisor(质因数分解)

    题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...

随机推荐

  1. Docker源码分析(六):Docker Daemon网络

    1. 前言 Docker作为一个开源的轻量级虚拟化容器引擎技术,已然给云计算领域带来了新的发展模式.Docker借助容器技术彻底释放了轻量级虚拟化技术的威力,让容器的伸缩.应用的运行都变得前所未有的方 ...

  2. js 数组函数

    Array.prototype.join Array.prototype.reverse Array.prototype.sort Array.prototype.concat Array.proto ...

  3. Jenkins反序列化漏洞cve-2017-1000353

    一.漏洞原理: 本地没有环境:参考:https://blogs.securiteam.com/index.php/archives/3171    进行学习理解记录. 首先这是一个java反序列化漏洞 ...

  4. EUI组件之ProgressBar

    一.ProgressBar常规使用 拖动一个progressbar到exml 代码中使用 /** * 主页场景 * @author chenkai 2018/5/26 */ class HomeSce ...

  5. Egret打包App 修改App名称和图标 (Egret4.1.0)

    图标替换位置在项目res下的drawable这些目录下,用新图标覆盖即可. 这里我用白色图片替换了白鹭默认的图片ic_launcher.png 修改App名字,在res->value->s ...

  6. 把 Activity 改成 ListActivity继续使用 setContentView

    ListActivity has a default layout that consists of a single, full-screen list in the center of the s ...

  7. idea的svn插件中compare with the same repository version和compare with latest repository version的区别?

    Idea的svn插件中compare with the same repository version和compare with latest repository version的区别? 1.com ...

  8. ZOJ 3202 Second-price Auction

    Second-price Auction Time Limit: 1 Second      Memory Limit: 32768 KB Do you know second-price aucti ...

  9. java 中的控制台端口的输入

    java 中基于控制台端的输入时最基本的操作. 第一步 导入Scanner类 import java.util.Scanner; 第二步 创建输入对象 input Scanner input=new ...

  10. Qt::浅谈信号槽连接,参数在多线程中的使用

    Qt的信号槽有五种连接方式定义在enum Qt::ConnectionType,下面简单介绍 Qt::AutoConnection:自动判断连接方式,如果信号发送对象和执行槽对象在同一线程,那么等于Q ...