Problem - 7390

题目大意:有一个n个数的数组a,对他们的所有非空子序列求异或和得到长度为XOR Subsequence 2023“钉耙编程”中国大学生算法设计超级联赛(10)hdu7390-LMLPHP的数组b,给出b,求a

1<=n<=18

思路:可以发现,a数组其实是b数组的线性基

XOR Subsequence 2023“钉耙编程”中国大学生算法设计超级联赛(10)hdu7390-LMLPHP

线性基详解_Hypoc_的博客-CSDN博客) 

那么直接求b数组的线性基即可,要注意的是线性基中数的个数肯能不足n个,要用0来补

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+10;
ll n;
bool zero;
ll a[N];
void gauss()
{
    int i,k=1;
    ll j = (ll)1<<30;
    for(;j;j>>=1)
    {
        for(i=k;i<=n;i++)
        {
            if(a[i]&j) break;
        }
        if(i>n) continue;
        swap(a[i],a[k]);
        for(i=1;i<=n;i++)
        {
            if(i!=k&&a[i]&j) a[i]^=a[k];
        }
        k++;
    }
    k--;
    if(k!=n) zero=true;
    else zero = false;
    n=k;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll temp = ((ll)1<<n)-1;
        swap(n,temp);
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            sum^=a[i];
        }
        if(n==1)
        {
            cout<<a[1]<<endl;
            continue;
        }
        if(sum!=0)
        {
            cout<<"-1"<<endl;
            continue;
        }
        gauss();
        while(temp>n) cout<<0<<" ",temp--;
        for(int i=n;i>=1;i--)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
08-19 18:01