QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114767 | #6194. Knapsack Problem | std_abs# | WA | 2ms | 3544kb | C++14 | 2.6kb | 2023-06-23 15:38:01 | 2023-06-23 15:38:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 998244353, N = 400005;
typedef pair<int, int> pi;
int c[16], a[4];
vector<pair<vector<int>, int>> s[16];
void solve() {
int k;
int x[16];
cin >> k;
for (int i = 1; i < 1 << k; ++i)
cin >> c[i], x[i] = 0;
for (int i = 0; i < k; ++i)
cin >> a[i], x[1 << i] = a[i];
//for (int _ = 0; _ < 100; ++_) {
for (int i = 3; i < 1 << k; ++i)
for (auto &[ss, y] : s[i]) {
ll tmp = 1ll * y * c[i];
int mn = 1e9;
for (int j : ss)
tmp -= c[j], mn = min(mn, x[j]);
if (tmp > 0) {
x[i] += 1ll * mn * y;
for (int j : ss)
x[j] -= mn;
}
else if (tmp < 0) {
for (int j : ss)
x[j] += x[i] / y;
x[i] %= y;
}
}
//}
ll ans = 0;
for (int i = 1; i < 1 << k; ++i)
ans += 1ll * c[i] * x[i];
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int cnt[16]{};
for (int i = 0; i < 16; ++i)
for (int j = 0; j < 4; ++j)
cnt[i] += (i >> j & 1) << 4 * j;
for (int i = 1; i < 1 << 16; ++i) {
if (__builtin_popcount(i) == 1 || i & 1)
continue;
int tmp = 0;
for (int j = 0; j < 16; ++j)
if (i >> j & 1)
tmp += cnt[j];
int x = 0, y = 0;
for (int j = 0; j < 4; ++j)
if (tmp >> 4 * j & 15) {
if (y != 0 && y != (tmp >> 4 * j & 15)) {
x = -1;
break;
}
else if (!y)
y = tmp >> 4 * j & 15;
x += 1 << j;
}
if (x != -1 && !(i >> x & 1)) {
vector<int> vv;
for (int j = 0; j < 16; ++j)
if (i >> j & 1)
vv.push_back(j);
s[x].emplace_back(vv, y);
}
}
int t;
cin >> t;
while (t--) {
solve();
}
}
/*
3
2
1 2 4
4 5
3
3226252 19270256 2430652 1187613 12496062 10286165 17494834
24 85 34
4
901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555
529321239 218214127 92942310 207467810
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3540kb
input:
3 2 1 2 4 4 5 3 3226252 19270256 2430652 1187613 12496062 10286165 17494834 24 85 34 4 901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555 529321239 218214127 92942310 207467810
output:
18 1949753378 7832404777567179
result:
ok 3 number(s): "18 1949753378 7832404777567179"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3544kb
input:
100 4 4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493 200477197 258791439 590664017 982949628 4 5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...
output:
16156444320083421 24565409716449369 29418799514760027 30685726224479370 21026116646195695 27272325550817430 28830991867949938 34577148024284057 9058893580144422 21525085254465501 6657186892229297 16750622568043659 11217842843383520 12674703085309534 33873234230125448 3890237279573366 319038722503053...
result:
wrong answer 2nd numbers differ - expected: '24565535429631234', found: '24565409716449369'