codeforces 1582 F. Korney Korneevich and XOR

本文介绍了一种优化的算法来解决F.KorneyKorneevich问题,即在给定长度n的数列中,计算所有递增子序列的不同异或值。原始代码中存在复杂度较高的问题,通过改进策略,降低了计算复杂度并提高了效率。
摘要由CSDN通过智能技术生成

F. Korney Korneevich and XOR (hard version)

传送门

给一个长度为n的数列a[],求所有递增子序列的不同的异或值

a i ≤ 5000 , n ≤ 1 e 6 a_i \le 5000, n\le 1e6 ai5000,n1e6

F1 的 subtask, a i ≤ 500 , n ≤ 1 e 5 a_i \le 500 , n \le 1e5 ai500,n1e5 直接向后转移

#include <bits/stdc++.h>

using namespace std;

const int N = 550;
const int inf = 0x3f3f3f3f;
int f[N];

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    memset(f, 0x3f, sizeof f);
    f[0] = 0;

    int n;
    cin >> n;
    for (int i = 1, x; i <= n; ++i) {
        cin >> x;
        for (int j = 0; j < N; ++j) {
            if (f[j] < x)
                f[j ^ x] = min(f[j ^ x], x);
        }
    }

    vector<int> res;
    for (int i = 0; i < N; ++i)
        if (f[i] != inf) res.push_back(i);

    cout << res.size() << endl;
    for (auto x: res) cout << x << " ";
}

记录每个ai的位置

复杂度:5000 * (1 << 13) * math.log(1e6) 其实挺卡的

#include <bits/stdc++.h>

using namespace std;

const int N = (1 << 13);
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
int f[N]; // f[i]:=异或和为i的序列中最后一个数最小是多少
vector<int> g[N]; //g[i]:= i的位置

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    memset(f, 0x3f, sizeof f);
    f[0] = 0;

    int n;
    cin >> n;
    for (int i = 1, x; i <= n; ++i) {
        cin >> x;
        g[x].push_back(i);
    }

    for (int i = 0; i <= 5000; ++i) {
        for (int j = 0; j < N; ++j) {
            auto it = upper_bound(g[i].begin(), g[i].end(), f[j]);
            if (it != g[i].end()) f[i ^ j] = min(f[i ^ j], (*it));
        }
    }

    vector<int> res;
    for (int i = 0; i < N; ++i)
        if (f[i] != inf) res.push_back(i);

    cout << res.size() << endl;
    for (auto x: res) cout << x << " ";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值