QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#121910 | #6194. Knapsack Problem | UndertrainedOverpressure# | TL | 54ms | 8356kb | C++23 | 7.0kb | 2023-07-09 00:24:21 | 2023-07-09 00:24:23 |
Judging History
answer
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)
int sign(vector<int> p) {
int res = 1;
for (int i = 0; i < (int)p.size(); i++) {
if (i != p[i]) {
swap(p[i], p[p[i]]);
res = -res;
}
}
return res;
}
map<vector<int>, int> cache2[5];
int getDet(int k, const vector<int>& masks) {
auto it = cache2[k].find(masks);
if (it != cache2[k].end()) {
return it->second;
}
vector<int> p(k);
iota(p.begin(), p.end(), 0);
int ans = 0;
do {
int mul = 1;
for (int i = 0; i < k; i++) {
if (((masks[i] >> p[i]) & 1) == 0) {
mul = 0;
break;
}
}
if (mul == 1) {
ans += sign(p);
}
} while (next_permutation(p.begin(), p.end()));
return (cache2[k][masks] = ans);
}
void check() {
int k;
cin >> k;
int best = 0;
int cnt = 0;
for (int mask = 0; mask < (1 << (1 << k)); mask += 2) {
if (__builtin_popcount(mask) == k) {
vector<int> masks;
for (int i = 0; i < (1 << k); i++) {
if ((mask >> i) & 1) {
masks.emplace_back(i);
}
}
int cur = getDet(k, masks);
if (cur == 0) {
continue;
}
for (int i : masks) {
for (int j = 0; j < k; j++) {
cerr << ((i >> j) & 1);
}
cerr << "\n";
}
cerr << cur << "\n";
best = max(best, abs(cur));
cnt++;
}
}
cerr << best << " " << cnt << "!!!\n";
}
map<vector<int>, vector<vector<int>>> cache[5];
vector<vector<int>> inv(int k, const vector<int>& masks) {
auto it = cache[k].find(masks);
if (it != cache[k].end()) {
return it->second;
}
vector<vector<int>> ans(k, vector<int>(k, 0));
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
vector<int> new_masks;
for (int t = 0; t < k; t++) {
if (t != j) {
int cur = masks[t] & ((1 << i) - 1);
cur += (masks[t] >> (i + 1)) << i;
new_masks.emplace_back(cur);
}
}
ans[j][i] = getDet(k - 1, new_masks);
if ((i + j) % 2 == 1) {
ans[j][i] *= -1;
}
}
}
return (cache[k][masks] = ans);
}
ll dp[390625];
int to_mask[16];
void solve() {
int k;
cin >> k;
vector<int> c((1 << k), 0);
for (int i = 1; i < (1 << k); i++) {
cin >> c[i];
}
vector<int> a(k, 0);
for (int i = 0; i < k; i++) {
cin >> a[i];
}
ll ans = 0;
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 1; i < (1 << k); i++) {
for (int t = 390624; t >= 0; t--) {
if (dp[t] != -1) {
for (int u = 0; u < 2; u++) {
dp[t + (u + 1) * to_mask[i]] = max(dp[t + (u + 1) * to_mask[i]], dp[t] + (u + 1) * c[i]);
}
}
}
}
vector<vector<int>> cur_vecs;
vector<int> cur_vals;
for (int t = 0; t < 390625; t++) {
if (dp[t] != -1) {
cur_vals.emplace_back(dp[t]);
cur_vecs.emplace_back();
cur_vecs.back().reserve(k);
int pw = 1;
for (int i = 0; i < k; i++) {
cur_vecs.back().emplace_back((t / pw) % 25);
pw *= 25;
}
}
}
for (int mask = 0; mask < (1 << (1 << k)); mask += 2) {
if (__builtin_popcount(mask) == k) {
vector<int> masks;
for (int i = 0; i < (1 << k); i++) {
if ((mask >> i) & 1) {
masks.emplace_back(i);
}
}
int cur = getDet(k, masks);
if (cur == 0) {
continue;
}
auto cur_inv = inv(k, masks);
int right_bound = cur_vecs.size();
if (abs(cur) == 1) {
right_bound = 1;
}
for (int iter = 0; iter < right_bound; iter++) {
const auto& delta = cur_vecs[iter];
ll add = cur_vals[iter];
bool ok = true;
for (int j = 0; j < k; j++) {
if (a[j] < delta[j]) {
ok = false;
continue;
}
if ((a[j] - delta[j]) % abs(cur) != 0) {
ok = false;
continue;
}
}
if (!ok) {
continue;
}
vector<ll> cur_x(k, 0);
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
cur_x[i] += (ll)cur_inv[i][j] * ((a[j] - delta[j]) / abs(cur));
}
if (cur < 0) {
cur_x[i] *= -1;
}
if (cur_x[i] < 0) {
ok = false;
break;
}
}
if (ok) {
for (int i = 0; i < k; i++) {
add += (ll)c[masks[i]] * cur_x[i];
}
ans = max(ans, add);
}
}
}
}
cout << ans << "\n";
}
int main() {
#ifdef ONPC
freopen("input", "r", stdin);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
/*for (int k = 2; k <= 4; k++) {
for (int cur = 1; cur <= 4; cur++) {
for (int ms = 0; ms < 390625; ms++) {
vector<int> vc;
int pw = 1;
for (int i = 0; i < k; i++) {
vc.emplace_back((ms / pw) % 25);
pw *= 25;
}
for (int& x : vc) {
x %= cur;
}
int idx = 0;
pw = 1;
for (int i = 0; i < k; i++) {
idx += vc[i] * pw;
pw *= cur;
}
go[k][cur][idx].emplace_back(ms);
}
}
}*/
for (int i = 0; i < (1 << 4); i++) {
to_mask[i] = 0;
int pw = 1;
for (int x = 0; x < 4; x++) {
if ((i >> x) & 1) {
to_mask[i] += pw;
}
pw *= 25;
}
}
int t = 1;
cin >> t;
while (t--) {
double start_time = TIME;
solve();
cerr << (TIME - start_time) << "\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 54ms
memory: 8356kb
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
Time Limit Exceeded
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 73696042855729205 29418802996321901 30685727712782520 63078328543662915 27272339796311010 28831028447377015 103731405044630742 9058894962996375 21525085254465501 6657186892229297 16750628911275484 33653172846387390 12674726744043780 33873234230125448 3890237279573366 95711386356687...