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

  1. problem link:http://codeforces.com/contest/1025/problem/B
  2. 题意:给你又一个n和n对数,找到每对数乘积的都含有的一个因数,如果没有输出-1,如果有输出任意的一个。
  3. 解题思路:先找到每对数的最大公倍数,然后再与每对数进行gcd,过滤得到最终结果。
  4. AC code:
#include<iostream>
using namespace std;
typedef long long ll;
const int N=15e4+6;
pair<ll,ll>itv[N];
ll gcd(ll a,ll b){if(!b)return a;return gcd(b,a%b);}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    int n;cin>>n;
    for(int i=1;i<=n;i++)cin>>itv[i].first>>itv[i].second;
    ll res=itv[1].first*itv[1].second;
    for(int i=2;i<=n;i++)res=gcd(res,itv[i].first*itv[i].second);
    for(int i=1;i<=n;i++){
        int x=gcd(res,itv[i].first),y=gcd(res,itv[i].second);
        if(x==1)res=y;else res=x;
    }
    res=res==1?-1:res;cout<<res<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值