結果

問題 No.3502 GCD Knapsack
コンテスト
ユーザー amano-amane
提出日時 2026-04-18 17:58:27
言語 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
結果
WA  
実行時間 -
コード長 1,895 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,309 ms
コンパイル使用メモリ 380,336 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2026-04-18 17:59:10
合計ジャッジ時間 8,734 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 3 WA * 2 TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// Compile: g++ -std=c++20 -O2 -Wall -I$HOME/ctf/tools/ac-library -DLOCAL -o sol sol.cpp
// Or:      make sol   (uses Makefile in this dir)
// Run:     ./sol < in.txt
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;

#define rep(i,n) for(ll i=0;i<(ll)(n);++i)
#define rep2(i,a,b) for(ll i=(ll)(a);i<(ll)(b);++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) ((ll)(x).size())

template<class T> bool chmin(T&a,const T&b){if(b<a){a=b;return true;}return false;}
template<class T> bool chmax(T&a,const T&b){if(a<b){a=b;return true;}return false;}

#ifdef LOCAL
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#else
#define dbg(x)
#endif

using mint = modint998244353;
// using mint = modint1000000007;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, w;
    cin >> n >> w;
    vector<ll> x(n), y(n);
    rep(i, n) cin >> x[i];
    rep(i, n) cin >> y[i];

    map<ll, ll> g; // 重み(x), 価値(y)
    
    for (int i = 0; i < n; i++) {
        map<ll, ll> g_new = g;
        for (auto p : g) {
            ll new_gcd = gcd(p.first, x[i]);
            if(new_gcd >= w){   
                if(p.second+y[i] > g[new_gcd] ){
                    g_new[new_gcd] = p.second + y[i];
                }
            }
        }
        if(x[i] >= w){  
            if(y[i] > g[x[i]]){
                g_new[x[i]] = y[i];
            }
        }
        g = g_new;
    }
    ll y_max = 0;
    for(auto p: g) {
        if(p.first >= w){
            // cout << p.first << ", " << p.second << endl;
            chmax(y_max, p.second);
        }
    }
    cout << y_max << endl;
    return 0;
}
0