結果
| 問題 | No.3670 Fast Knapsack |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-09-05 17:27:25 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 37 ms / 2,500 ms |
| + 949µs | |
| コード長 | 1,893 bytes |
| 記録 | |
| コンパイル時間 | 2,958 ms |
| コンパイル使用メモリ | 367,592 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2026-09-05 17:27:35 |
| 合計ジャッジ時間 | 6,399 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int MAXLEN = 1 << 18;
template<int len> int solve(int N, int S, vector<int> &a) {
if (len <= S) return solve<min(len * 2, MAXLEN)>(N, S, a);
bitset<len> dp;
dp[0] = 1;
int i = 0;
while(i < N && 2 * a[i] <= S) {
int p = i;
while(i < N && a[i] == a[p]) i++;
int cnt = i - p;
for(int take = 1; cnt > 0; take *= 2) {
int D = min(take, cnt);
long long shift = (ll)(a[p]) * D;
cnt -= D;
if(shift <= S) dp |= dp << shift;
}
}
int j = S;
while(!dp[j]) j--;
int ans = j;
while(i < N){
j = min(j, S - a[i]);
while(!dp[j]) j--;
ans = max(ans, j + a[i++]);
}
return ans;
}
array<int, 1 << 14> tb;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int N, S, g = 0;
cin >> N >> S;
vector<int> a(N);
for(auto &&v : a) cin >> v;
sort(a.begin(), a.end());
while(!a.empty() && a.back() > S) a.pop_back();
N = a.size();
ll tot = 0;
for(auto &&v : a){
tot += v;
g = gcd(g, v);
}
if(tot <= S){
cout << tot << '\n';
continue;
}
if(N <= 14){
int ans = 0;
for(int i = 0; i < N; i++) tb[1 << i] = a[i];
for(int i = 1; i < (1 << N); i++){
int b = i & -i;
tb[i] = tb[i ^ b] + tb[b];
if(tb[i] <= S) ans = max(ans, tb[i]);
}
cout << ans << '\n';
continue;
}
if(g >= 2){
S /= g;
for(auto &&v : a) v /= g;
}
cout << g * solve<16>(N, S, a) << '\n';
}
}