7.23 补题

I - LCM of GCDs

题意

n组数,每组包含a[i],b[i],每组将两个数放到不同的盒A,B中问A中B中所有数的最大公约数的最小共倍数。

思路

假设两组数的gcd分别为x, y。第一组数一定会放到不同的盒子中,并且这两个数的因数一定是各自盒子中所有数的因数。假设因数数量分别为na,nb。所以最后的x和y一定在na*nb种选择种。枚举每种选择,再所有数中判断是否都能被整除。最后维护最大值即可。

#define _CRT_SECURE_NO_WARNINGS 1;
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
#define off ios::sync_with_stdio(false);
#define INF 0x3f3f3f3f
#define forn(i,n) for(int i = 0; i < int(n); i++)
#define scan1(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d %d",&n,&m)
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define debug(x) cout <<'*' << x <<endl;

const int N = 1010;
const int M = 2 * N;
const int mod = 998244353;
const int P = 131;
typedef pair<int, int> PII;
typedef long long ll;
typedef vector<int> vic;

int a[55], b[55];
vic diva;
vic divb;

int gcd(int a, int b) {
    return (a % b == 0) ? b : gcd(b, a % b);
}

vic get_div(int x) {
    vic ans;
    for (int i = 1; i <= x / i; i++) {
        if (x % i == 0) {
            ans.push_back(i);
            if (i != x / i) ans.push_back(x / i);
        }
    }
    return ans;
}

ll lcm(int a, int b) {
    return (ll)a * b / gcd(a, b);
}

int main()
{
    off;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    diva = get_div(a[0]);
    divb = get_div(b[0]);
    ll ans = 0;
    for (auto x : diva) {
        for (auto y : divb) {
            int f = 1;
            for (int i = 0; i < n; i++) {
                if (a[i] % x == 0 && b[i] % y == 0) continue;
                if (b[i] % x == 0 && a[i] % y == 0) continue;
                f = 0;
                break;
            }
            if (f) {
                ans = max(ans, lcm(x, y));
            }
        }
    }
    
    cout << ans << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值