結果

問題 No.3502 GCD Knapsack
コンテスト
ユーザー Azaki
提出日時 2026-04-18 04:16:14
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 1,097 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,319 ms
コンパイル使用メモリ 177,152 KB
実行使用メモリ 34,560 KB
最終ジャッジ日時 2026-04-18 04:16:41
合計ジャッジ時間 8,948 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

/**
 * Bài toán: GCD Knapsack (GCD >= W)
 * Logic: GCD của tập được chọn phải là g >= W.
 * Điều này có nghĩa là mọi w_i trong tập đó phải chia hết cho g.
 */

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

    int N;
    long long W;
    cin >> N >> W;

    map<long long, long long> gcd_sums;

    for (int i = 0; i < N; ++i) {
        long long w, v;
        cin >> w >> v;

        if (w < W) continue;

        // Tìm tất cả ước số d của w sao cho d >= W
        for (long long d = 1; d * d <= w; ++d) {
            if (w % d == 0) {
                if (d >= W) {
                    gcd_sums[d] += v;
                }
                if (w / d != d && (w / d) >= W) {
                    gcd_sums[w / d] += v;
                }
            }
        }
    }

    long long max_val = 0;
    for (auto const& [g, total_v] : gcd_sums) {
        max_val = max(max_val, total_v);
    }

    cout << max_val << endl;

    return 0;
}
0