QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121887#6194. Knapsack ProblemUndertrainedOverpressure#WA 83ms4124kbC++236.2kb2023-07-08 23:31:102023-07-08 23:31:14

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-08 23:31:14]
  • 评测
  • 测评结果:WA
  • 用时:83ms
  • 内存:4124kb
  • [2023-07-08 23:31:10]
  • 提交

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

int getDet(int k, const vector<int>& masks) {
    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 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);
}

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;
    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;
            }
            ll add = 0;
            //cerr << add << "!!!!!!!!!!\n";
            auto cur_inv = inv(k, masks);

            /*for (int i : masks) {
                for (int j = 0; j < k; j++) {
                    cerr << ((i >> j) & 1);
                }
                cerr << "\n";
            }
            cerr << cur << "\n";
            for (int i = 0; i < k; i++) {
                for (int j = 0; j < k; j++) {
                    cerr << cur_inv[i][j] << " ";
                }
                cerr << "\n";
            }
            cerr << "\n";*/

            bool ok = true;
            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] / abs(cur));
                }
                if (cur < 0) {
                    cur_x[i] *= -1;
                }
                if (cur_x[i] < 0) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                if (abs(cur) > 1) {
                    vector<int> delta(a);
                    for (int i = 0; i < k; i++) {
                        delta[i] %= abs(cur);
                    }
                    map<vector<int>, ll> best;
                    best[delta] = 0;
                    for (int i = 1; i < (1 << k); i++) {
                        map<vector<int>, ll> cur_best = best;
                        for (auto t : best) {
                            vector<int> new_t(t.first);
                            bool stop = false;
                            for (int u = 0; u < 10; u++) {
                                for (int x = 0; x < k; x++) {
                                    if ((i >> x) & 1) {
                                        new_t[x]--;
                                        if (new_t[x] < 0) {
                                            stop = true;
                                            break;
                                        }
                                    }
                                }
                                if (stop) {
                                    break;
                                }
                                cur_best[new_t] = max(cur_best[new_t], t.second + (u + 1) * c[i]);
                            }
                        }
                        swap(best, cur_best);
                    }
                    add = best[vector<int>(k, 0)];
                }

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

    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: 0ms
memory: 4124kb

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: 83ms
memory: 3996kb

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...

result:

wrong answer 2nd numbers differ - expected: '24565535429631234', found: '73696042855729205'