QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121942#6194. Knapsack ProblemUndertrainedOverpressure#RE 280ms14428kbC++238.4kb2023-07-09 01:07:212023-07-09 01:07:23

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-09 01:07:23]
  • 评测
  • 测评结果:RE
  • 用时:280ms
  • 内存:14428kb
  • [2023-07-09 01:07:21]
  • 提交

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];

mt19937 rnd(239);

vector<int> pos_int[5][20];

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++) {
        if (pos_int[k][i].empty()) {
            for (int t = 390624; t >= 0; t--) {
                if (dp[t] != -1) {
                    pos_int[k][i].emplace_back(t);
                    for (int u = 0; u < 3; u++) {
                        dp[t + (u + 1) * to_mask[i]] = max(dp[t + (u + 1) * to_mask[i]], dp[t] + (u + 1) * c[i]);
                    }
                }
            }
        } else {
            //cerr << pos_int[k][i].size() << "\n";
            for (int t : pos_int[k][i]) {
                for (int u = 0; u < 3; 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;
    cur_vecs.reserve(pos_int[k][0].size());
    cur_vals.reserve(pos_int[k][0].size());
    if (pos_int[k][0].empty()) {
        for (int t = 0; t < 390625; t++) {
            if (dp[t] != -1) {
                pos_int[k][0].emplace_back(t);
                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;
                }
            }
        }
    } else {
        for (int t : pos_int[k][0]) {
            if (dp[t] != -1) {
                pos_int[k][0].emplace_back(t);
                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;
                }
            }
        }
    }

    vector<tuple<int, vector<int>, int>> elems;
    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;
            }
            elems.emplace_back(mask, masks, cur);
        }
    }

    shuffle(elems.begin(), elems.end(), rnd);
    for (auto [mask, masks, cur] : elems) {
        auto cur_inv = inv(k, masks);

        ll ans_best = 0;
        for (int i = 1; i < (1 << k); i++) {
            ans_best += 3 * c[i];
        }
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < k; j++) {
                ans_best += (ll) cur_inv[i][j] * ((a[j]) / abs(cur)) * c[masks[i]];
            }
        }
        if (ans_best < ans) {
            continue;
        }


        for (int iter = 0; iter < cur_vecs.size(); 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 280ms
memory: 14428kb

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
Runtime Error

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:

16156443117220257
24565512875059188
29418802996321901

result: