結果

問題 No.3461 Min GCD
コンテスト
ユーザー D M
提出日時 2026-11-18 14:36:35
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 85 ms / 2,000 ms
+ 535µs
コード長 1,535 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,448 ms
コンパイル使用メモリ 345,376 KB
実行使用メモリ 14,592 KB
最終ジャッジ日時 2026-11-18 14:36:41
合計ジャッジ時間 5,786 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    ll K;
    cin >> N >> K;

    vector<int> A(N), B(N);

    for (auto &a : A) cin >> a;
    for (auto &b : B) cin >> b;

    int M = *max_element(A.begin(), A.end());
    int U = *min_element(A.begin(), A.end());

    // 各整数の約数を昇順で列挙
    vector<vector<int>> divisors(M + 1);

    for (int d = 1; d <= M; d++) {
        for (int j = d; j <= M; j += d) {
            divisors[j].push_back(d);
        }
    }

    // 差分配列
    vector<ll> diff(U + 2, 0);

    for (int i = 0; i < N; i++) {
        const auto &ds = divisors[A[i]];

        int best = INT_MAX;

        // 約数を大きい順に処理
        for (int j = (int)ds.size() - 1; j >= 0; j--) {
            int d = ds[j];

            // B[i] を d の倍数にする最小操作回数
            int cost = (d - B[i] % d) % d;

            best = min(best, cost);

            // この best が適用されるスコアの区間
            int L = (j == 0 ? 1 : ds[j - 1] + 1);
            int R = min(d, U);

            if (L <= R) {
                diff[L] += best;
                diff[R + 1] -= best;
            }
        }
    }

    // 各スコアに必要な操作回数を計算
    ll sum = 0;
    int ans = 1;

    for (int x = 1; x <= U; x++) {
        sum += diff[x];

        if (sum <= K) {
            ans = x;
        }
    }

    cout << ans << '\n';

    return 0;
}
0