题意:给你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. java基础---->FilenameFilter之文件过滤

    FilenameFilter用于对列表中文件名的过滤,今天我们就开始java中FilenameFilter的学习.好多年了,你一直在我的伤口中幽居,我放下过天地,却从未放下过你,我生命中的千山万水,任 ...

  2. android基础组件---->Checkboxe的使用

    由于使用比较简单,这篇博客涵盖Checkboxes和Radio Buttons和Toggle Buttons.好了我们开始今天的学习.我被世俗隐瞒,转身又被自己撞倒.从莫须有的罪名起步,行色简单,心术 ...

  3. 【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor

    Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom),四个参数类型都是drawa ...

  4. [MongoDB] 安装MongoDB配置Replica Set

    MongoDB的环境主要包括StandAlone,Replication和Sharding. StandAlone:单机环境,一般开发测试的时候用. Replication:主从结构,一个Primar ...

  5. Oracle涂抹oracle学习笔记第8章RMAN说,我能备份

    本次测试服务器为172.16.25.33 使用rman连接本地数据库 rman target / 在rman中执行启动与关闭的命令与sqlplus相同 在rman中执行sql语句 sql ‘需要执行的 ...

  6. Django - ORM - 进阶

    一.多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是 ...

  7. [ jQuery ] scrollTop与offset()!

    jQuery scrollTop() 与offset()! 曾经很长一段时间,很多人问我下面一段代码的好处是什么? ;(function($){ //do something })(jQuery); ...

  8. 滚动侦测scrollspy

    <!doctype html><html> <head><meta charset="utf-8"><meta http-eq ...

  9. input输入框type=file时accept中可以限制的文件类型(转载)

    转载自: input type=file accept中可以限制的文件类型 在上传文件的时候,需要限制指定的文件类型. <input type="file" accept=& ...

  10. 剑指offer-有序二维数组中的查找

    在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. def Find(self, t ...