結果
| 問題 | No.3670 Fast Knapsack |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-09-05 14:24:47 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 112 ms / 2,500 ms |
| + 168µs | |
| コード長 | 1,976 bytes |
| 記録 | |
| コンパイル時間 | 3,178 ms |
| コンパイル使用メモリ | 366,076 KB |
| 実行使用メモリ | 6,272 KB |
| 最終ジャッジ日時 | 2026-09-05 14:25:21 |
| 合計ジャッジ時間 | 6,091 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#include <bits/stdc++.h>
using namespace std;
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;
for(i = 0; 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;
if(dp[S]) return S;
int ans = 0;
while(j >= 0 && !dp[j])j--;
ans = j;
while(i < N){
while(a[i] + j > S) j--;
while(j >= 0 && !dp[j]) j--;
if(j < 0) return ans;
ans = max(ans, j + a[i++]);
}
return ans;
}
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);
ll tot = 0;
for(auto &&v : a) cin >> v, tot += v;
if(tot <= S){
cout << tot << '\n';
continue;
}
sort(a.begin(), a.end());
while(!a.empty() && a.back() > S) a.pop_back();
N = a.size();
if(N <= 12){
int ans = 0;
for(int i = 1; i < (1 << N); i++){
int sv = 0;
for(int j = 0; j < N; j++){
if(i >> j & 1) sv += a[j];
}
if(sv <= S) ans = max(ans, sv);
}
cout << ans << '\n';
continue;
}
for(auto &&v : a) g = gcd(g, v);
if(g >= 2){
S /= g;
for(auto &&v : a) v /= g;
}
cout << g * solve<16>(N, S, a) << '\n';
}
}