Linear-based foundation problems

Big brother blog
Suppose the number n, the n number can be composed of a collection of exclusive or and is V, a linear group is able to represent the minimal set of set V and XOR.
Linear action group: Solution XOR and the k small, XOR and maximum, is present in a number of XOR and the collection problems.
Luo Gu P3812
maximum and the divergent or

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
typedef long long ll;
ll a[maxn], b[65], ans;
int n, m;
void prepare() {
    int cnt = 0;
    memset(b, 0, sizeof(b));
    for (int i = 1; i <= n; i++) {
        for (int j = 62; j >= 0; j--) {
            if((a[i]>>j)&1) {
                if(b[j]) a[i] ^= b[j];
                else {
                    b[j] = a[i]; cnt++;
                    for (int k = j - 1; k >= 0; k--)
                        if(b[k] && ((b[j] >>k)&1)) b[j] ^= b[k];
                    for (int k = j + 1; k <= 62; k++)
                        if((b[k]>>j)&1) b[k] ^= b[j];
                    break;
                }
            }
        }
    }
    for (int i = 0; i <= 62; i++) ans ^= b[i];
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    prepare();
    printf("%lld\n", ans);
    return 0;
}

Divergent or small k-
topic Link
meaning of the questions
to the number n, Always ask XOR and all the k small numbers.
Thought
it is a question of a linear group, will be established all data into a linear base group, say all linear group is not number 0 from low to high with k binary low to high corresponding to, all of k bits It is not exclusive and the k-th or less of the corresponding linear group 0.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
typedef long long ll;
ll a[maxn], b[65];
int zero, n, m;
vector<ll> v;
void prepare() {
    int cnt = 0;
    memset(b, 0, sizeof(b));
    for (int i = 1; i <= n; i++) {
        for (int j = 62; j >= 0; j--) {
            if((a[i]>>j)&1) {
                if(b[j]) a[i] ^= b[j];
                else {
                    b[j] = a[i]; cnt++;
                    for (int k = j - 1; k >= 0; k--)
                        if(b[k] && ((b[j] >>k)&1)) b[j] ^= b[k];
                    for (int k = j + 1; k <= 62; k++)
                        if((b[k]>>j)&1) b[k] ^= b[j];
                    break;
                }
            }
        }
    }
    zero = cnt != n;
    v.clear();
    for (int i = 0; i <= 62; i++)
        if(b[i]) v.push_back(b[i]);
}
ll query(ll x) {
    if(zero) x--;
    if(x >= (1ll<<v.size())) return -1;
    ll ans = 0;
    for (int i = 0; i < v.size(); i++)
        if((x>>i)&1) ans ^= v[i];
    return ans;
}
int main()
{
    int tt;
    scanf("%d", &tt);
    for (int t = 1; t <= tt; t++) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        zero = 0;
        prepare();
        scanf("%d", &m);
        printf("Case #%d:\n", t);
        while (m--) {
            ll x;
            scanf("%lld", &x);
            printf("%lld\n", query(x));
        }
    }
    return 0;
}

And is the first of several divergent or small
bzoj2844

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
typedef long long ll;
const int mod = 10086;
ll a[maxn], b[65];
int n, cnt;
vector<int> v;
void prepare() {
    memset(b, 0, sizeof(b));
    for (int i = 1; i <= n; i++) {
        for (int j = 62; j >= 0; j--) {
            if((a[i]>>j)&1) {
                if(b[j]) a[i] ^= b[j];
                else {
                    b[j] = a[i]; cnt++;
                    for (int k = j - 1; k >= 0; k--)
                        if(b[k] && ((b[j] >>k)&1)) b[j] ^= b[k];
                    for (int k = j + 1; k <= 62; k++)
                        if((b[k]>>j)&1) b[k] ^= b[j];
                    break;
                }
            }
        }
    }
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b) {
        if(b&1) ans = ans * a % mod;
        a = a * a % mod;
        b = b >> 1;
    }
    return ans;
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    prepare();
    for (int i = 0; i <= 62; i++)
        if(b[i]) v.push_back(i);
    ll q;
    scanf("%lld", &q);
    ll rk = 0;
    for (int i = 0; i < v.size(); i++)
        if(q>>v[i]&1) rk += 1 << i;
    //求出的rk其实是rank(q)-1,因为可以有空集,所以一定有0,线性基里是没有0的,所以rk就是rank(q)-1.
    printf("%lld\n", (qpow(2, n - cnt) * (rk) % mod + 1)%mod);
}

Published 26 original articles · won praise 2 · Views 408

Guess you like

Origin blog.csdn.net/D_Bamboo_/article/details/103599734