結果

問題 No.2570 最大最大公約数
ユーザー Yilin Fei
提出日時 2024-01-08 17:22:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 84,952 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-13 17:17:50
合計ジャッジ時間 2,990 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>

using ll = long long;

// Function to calculate the divisors of a number
std::set<ll> get_divisors(ll number) {
    std::set<ll> divisors;
    for (ll i = 1; i * i <= number; ++i) {
        if (number % i == 0) {
            divisors.insert(i);
            if (i != number / i) {
                divisors.insert(number / i);
            }
        }
    }
    return divisors;
}

// Function to calculate the minimum operations to make all elements divisible by x
ll calculate_operations(const std::vector<ll>& elements, ll x) {
    ll count = 0;
    for (ll element : elements) {
        count += std::min(element % x, x - element % x);
    }
    return count;
}

int main() {
    ll N, K;
    std::cin >> N >> K;
    std::vector<ll> A(N);

    for (ll& element : A) {
        std::cin >> element;
    }

    ll answer = 1;
    for (ll element : A) {
        auto divisors = get_divisors(element);
        for (ll divisor : divisors) {
            ll operations_needed = calculate_operations(A, divisor);
            if (operations_needed <= K) {
                answer = std::max(answer, divisor);
            }
        }
    }

    std::cout << answer << std::endl;
    return 0;
}
0