码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-20 14:27:22      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:iostream   fine   div   force   can   memset   ==   cst   double   

题意:

给你n对数,求一个数,可以让他整除每一对数的其中一个

思路:

枚举第一对数的质因数,然后暴力

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
    
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const db eps = 1e-7;
const int mod = 1e9+7;
const int maxn = 2e7+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);

inline int read(){
    int num;
    char ch;
    while((ch=getchar())<0 || ch>9);
    num=ch-0;
    while((ch=getchar())>=0 && ch<=9){
        num=num*10+ch-0;
    }
    return num;
}
int c = 0;
int p[maxn];
void d(int x){
    for(int i=2;1ll*i*i<=x;i++)if(x%i==0){
        p[c++]=i;
        while(x%i==0)x/=i;
    }
    if(x>1)p[c++]=x;
}
ll gcd(ll a, ll b){
    return b == 0 ? a : gcd(b, a % b);
}
PLL pa[150000 + 100];
bool cmp(PLL a, PLL b){
    return max(a.fst, a.sc) < max(b.fst, b.sc);
}
int main() {
    int n;
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++){
        scanf("%I64d %I64d", &pa[i].fst, &pa[i].sc);
    }
    //sort(pa, pa+n, cmp);
    d(pa[0].fst);
    d(pa[0].sc);
    if(n==1){
        printf("%I64d", pa[0].fst);
        return 0;
    }
    for(int i = 0; i < c; i++){
        int flg = 1;
        for(int j = 0; j < n && flg; j++){
            if(pa[j].fst%p[i]!=0 && pa[j].sc%p[i]!=0) flg = 0;

        }
        if(flg){
            printf("%d", p[i]);
            return 0;
        }
    }
    printf("-1");
    return 0;
}

 

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

标签:iostream   fine   div   force   can   memset   ==   cst   double   

原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9505108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!