【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]

【题解】

求出第一个数对的两个数他们有哪些质因子。

显然用这些质因子去试2..n就可以了。
看哪个可以满足 就输出对应的就可以了。

(一开始我求出这个数的所有因子(TLE了)。。其实没有必要。。。因为假设y是x的倍数。。然后y满足的话,显然x也能满足要求。。所以只要用质因子搞就行了。

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rson mid+1,r,rt<<1|1
using namespace std;

const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};

const int N = 15e4;

int n,m;
int a[N+10][2];
set<int> myset;
vector<int> v;

void get_yinzi(int x){
    int len = sqrt(x);
    for (int i = 1;i <= len;i++){
        if (x%i==0) {
            myset.insert(i);
            while (x%i==0 && i!=1 ) x/=i;
        }
    }
    if (x>1) myset.insert(x);
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    scanf("%d",&n);
    for (int i = 1;i <= n;i++) scanf("%d%d",&a[i][0],&a[i][1]);
    for (int i = 0;i <=1;i++) get_yinzi(a[1][i]);
    for (int x:myset) v.push_back(x);
    m=v.size();
    for (int i = 0;i < m;i++){
        int x = v[i];
        if (x<=1) continue;
        bool ok = true;
        for (int j = 2;j <= n;j++){
            if ((a[j][0]%x==0) || (a[j][1]%x==0)){
                continue;
            }else {
                ok = false;
                break;
            }
        }
        if (ok){
            printf("%d\n",x);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9504006.html

时间: 2024-08-30 05:50:31

【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor的相关文章

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)

B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mat

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

A : A. Doggo Recoloring time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colore

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 题解

真心简单的一场比赛 就是坑比较多(自己太蠢) A是一个水题 3分钟的时候过了 B也是一个比较简单的题 类似的套路见得多了 但是我当时可能比较困 想了一会才想出来 19分钟的时候过掉了 C同样很显然 性质不难发现 我在30分钟的时候通过了pretest 但是由于自己的愚蠢 忘记写了一句话 导致FST了... D本来是一个简单的dp题 但是我一直没往dp上想 在网络流上刚了1h之后终于换了思路 在1:45的时候通过了他 然后就时间不多了 E都没看 就去hack 成功hack了2个之后比赛就结束了 题

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: 1 #include<stdio.h> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 #include<set> 8 #inclu

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Array Restoration

D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn. Exactly qq querie

codeforces cf round#505(based on vk cup 2018 final) C. Plasticine zebra

构造题,把整个串想象成一个环.每次把环断开并反转的时候从切口处看进去的顺序是和刚开始从头到尾的顺序是一样的.于是每次不管如何翻转最后都要找到这个环上最大的连续子段长度 #include<bits/stdc++.h> using namespace std; string s; int main() { cin>>s; int tmp=s.size(); s=s+s; int ans=0; int len=1; for(int i=0;i<(int)s.size()-1;i++

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

In the year of 30XX30XX participants of some world programming championship live in a single large hotel. The hotel has nn floors. Each floor has mm sections with a single corridor connecting all of them. The sections are enumerated from 11 to mm alo

【枚举】【二分】【推导】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担给它们,要求每台服务器承担的资源需求不超过其所能提供的资源需求.给定一种合法的方案,每台服务器要么没有被分配给任何一个服务,或者被分配给其中一个服务. 对服务器按能提供的资源从小到大排序.枚举给S1分配的服务器数量i,然后在a数组中二分,就可以得到给S1提供的是哪i台服务器,它们占据了a数组中连续的