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

题意:

给你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;
}

猜你喜欢

转载自www.cnblogs.com/wrjlinkkkkkk/p/9505108.html