結果

問題 No.3502 GCD Knapsack
コンテスト
ユーザー D M
提出日時 2026-05-19 11:31:52
言語 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
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 2,955 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,523 ms
コンパイル使用メモリ 388,304 KB
実行使用メモリ 10,708 KB
最終ジャッジ日時 2026-05-19 11:32:05
合計ジャッジ時間 12,627 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, limit) for (long long i = (long long)x; i < (long long)limit; i++)
#define REP(i, x, limit) for (long long i = (long long)x; i <= (long long)limit; i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define el endl
#define spa " "
#define Yes cout << "Yes" << el
#define No cout << "No" << el
#define YES cout << "YES" << el
#define NO cout << "NO" << el
#define inp(x) for(auto &i:x)cin>>i
#define eps (1e-10)
#define Equals(a,b) (fabs((a) - (b)) < eps )
#define debug(x) cerr << #x << " = " << x << el
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vb = vector<bool>;
const double pi = 3.141592653589793238;
const int inf = 1073741823;
const ll infl = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const ll MOD = 998244353;
#include<atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using vm = vector<mint>;
// 約数列挙(O(√n))。n>=1 を想定。昇順で返す。
// 例: divisors(12) -> {1,2,3,4,6,12}
vector<ll> divisors(ll n){
    assert(n >= 1);
    vector<ll> small, large;
    for(ll i = 1; i*i <= n; ++i){
        if(n % i == 0){
            small.push_back(i);
            if(i * i != n) large.push_back(n / i);
        }
    }
    reverse(all(large));
    small.insert(small.end(), all(large));
    return small;
}

// --- 素因数分解→約数生成(必要なら) --- //
// trial division の素因数分解(O(√n))
vector<pair<ll,int>> factorize(ll n){
    assert(n >= 1);
    vector<pair<ll,int>> res;
    for(ll p = 2; p*p <= n; ++p){
        if(n % p == 0){
            int e = 0;
            while(n % p == 0){ n /= p; ++e; }
            res.emplace_back(p, e);
        }
    }
    if(n > 1) res.emplace_back(n, 1);
    return res;
}

// 素因数分解結果から全約数を生成(昇順)
vector<ll> divisors_from_factors(const vector<pair<ll,int>>& pf){
    vector<ll> ds = {1};
    for(auto [p, e] : pf){
        vector<ll> add;
        add.reserve(ds.size() * e);
        ll pe = 1;
        for(int k = 1; k <= e; ++k){
            pe *= p; // p^k
            for(ll x : ds) add.push_back(x * pe);
        }
        ds.insert(ds.end(), all(add));
    }
    sort(all(ds));
    return ds;
}

/* 使い方例:
ll n; cin >> n;
auto ds1 = divisors(n);
// または:
// auto pf = factorize(n);
// auto ds2 = divisors_from_factors(pf);
*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n,w;cin>>n>>w;
  vl X(n),Y(n);inp(X);inp(Y);
  unordered_map<ll,ll> mp;
  rep(i,0,n){
    ll &x=X[i],&y=Y[i];
    if(x<w) continue;
    for(ll d:divisors(x)){
      if(d>=w) mp[d]+=y;
    }
  }
  ll ans=0;
  for(auto &[k,v]:mp) ans=max(ans,v);
  cout<<ans<<el;
}
0