QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#121936#6194. Knapsack ProblemUndertrainedOverpressure#TL 72ms8608kbC++237.3kb2023-07-09 01:02:302023-07-09 01:02:33

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:02:33]
  • 评测
  • 测评结果:TL
  • 用时:72ms
  • 内存:8608kb
  • [2023-07-09 01:02:30]
  • 提交

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

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

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

详细

Test #1:

score: 100
Accepted
time: 72ms
memory: 8608kb

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:

16156443117220251
24565512875058975
29418802996321901
30685726224479430
21026118053726857
27272339796311010
28831028447377015
34577176834491128
9058894266171621
21525085254465501
6657185626333897
16750622568043729
11217846865301187
12674726744043780
33873234230125448
3890235805972970
319038665789454...

result: