結果

問題 No.3502 GCD Knapsack
コンテスト
ユーザー useless
提出日時 2026-04-18 20:50:50
言語 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,685 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,474 ms
コンパイル使用メモリ 184,068 KB
実行使用メモリ 340,916 KB
最終ジャッジ日時 2026-04-18 20:52:18
合計ジャッジ時間 16,465 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 5 TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using namespace std;

vector<long long> get_divisors(long long x) {
    vector<long long> ret;
    for (long long i = 1; i * i <= x; ++i) {
        if (x % i == 0) {
            ret.push_back(i);
            if (i * i != x) {
                ret.push_back(x / i);
            }
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

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

    int n;
    long long w;
    if (!(cin >> n >> w)) return 0;

    vector<long long> x(n), y(n);
    for (int i = 0; i < n; ++i) cin >> x[i];
    for (int i = 0; i < n; ++i) cin >> y[i];

    vector<long long> ss;
    for (int i = 0; i < n; ++i) {
        vector<long long> divs = get_divisors(x[i]);
        ss.insert(ss.end(), divs.begin(), divs.end());
    }
    sort(ss.begin(), ss.end());
    ss.erase(unique(ss.begin(), ss.end()), ss.end());

    int m = ss.size();
    vector<long long> dp(m, 0);

    for (int i = 0; i < n; ++i) {
        vector<long long> ndp = dp;
        long long xi = x[i];
        long long yi = y[i];

        for (int j = 0; j < m; ++j) {
            long long g = __gcd(xi, ss[j]);

            auto it = lower_bound(ss.begin(), ss.end(), g);
            int ni = distance(ss.begin(), it);

            if (dp[j] + yi > ndp[ni]) {
                ndp[ni] = dp[j] + yi;
            }
        }
        dp = move(ndp);
    }

    long long ans = 0;
    for (int i = 0; i < m; ++i) {
        if (ss[i] >= w) {
            if (dp[i] > ans) {
                ans = dp[i];
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0