結果

問題 No.2570 最大最大公約数
ユーザー eve__fuyukieve__fuyuki
提出日時 2023-12-08 16:16:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,566 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 214,004 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-08 16:16:45
合計ジャッジ時間 5,063 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
6,676 KB
testcase_01 AC 79 ms
6,676 KB
testcase_02 AC 90 ms
6,676 KB
testcase_03 AC 36 ms
6,676 KB
testcase_04 AC 37 ms
6,676 KB
testcase_05 AC 76 ms
6,676 KB
testcase_06 AC 86 ms
6,676 KB
testcase_07 AC 7 ms
6,676 KB
testcase_08 AC 9 ms
6,676 KB
testcase_09 AC 101 ms
6,676 KB
testcase_10 AC 101 ms
6,676 KB
testcase_11 AC 102 ms
6,676 KB
testcase_12 AC 101 ms
6,676 KB
testcase_13 AC 100 ms
6,676 KB
testcase_14 AC 66 ms
6,676 KB
testcase_15 AC 64 ms
6,676 KB
testcase_16 AC 36 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

int main() {
    fast_io();
    int n, k;
    cin >> n >> k;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.rbegin(), a.rend());
    long long ans = 1;
    vector<long long> divs;

    if (a[0] > k) {
        for (long long a0 = a[0] - k; a0 <= a[0] + k; a0++) {
            for (long long i = 1; i * i <= a0; i++) {
                if (a0 % i == 0) {
                    divs.push_back(i);
                    if (i * i != a0) {
                        divs.push_back(a0 / i);
                    }
                }
            }
        }

    } else {
        long long su = 0;
        for (int i = 0; i < n; i++) {
            su += a[i];
        }
        for (long long a0 = su - k; a0 <= su + k; a0++) {
            for (long long i = 1; i * i <= a0; i++) {
                if (a0 % i == 0) {
                    divs.push_back(i);
                    if (i * i != a0) {
                        divs.push_back(a0 / i);
                    }
                }
            }
        }
    }
    sort(divs.begin(), divs.end());
    divs.erase(unique(divs.begin(), divs.end()), divs.end());
    reverse(divs.begin(), divs.end());
    for (long long d : divs) {
        long long need = 0;
        for (int i = 0; i < n; i++) {
            need += min(a[i] % d, d - a[i] % d);
        }
        if (need <= k) {
            cout << d << endl;
            return 0;
        }
    }
}
0