http://acm.hdu.edu.cn/showproblem.php?pid=3949
求n个数的异或和第k小。
参考:https://blog.sengxian.com/algorithms/linear-basis
没了。
#include<cstdio> #include<iostream> #include<cstring> #include<vector> #include<algorithm> using namespace std; typedef long long ll; const int BASE=60; ll b[64]; int n,cnt; vector<ll>mp; ll query(ll k){ if(cnt!=n)k--; if(k>(1LL<<(int)mp.size())-1)return -1; ll ans=0; for(int i=0;i<(int)mp.size();i++){ if(k>>i&1)ans^=mp[i]; } return ans; } inline void init(){ cnt=0; memset(b,0,sizeof(b)); mp.clear(); } int main(){ int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++){ printf("Case #%d:\n",cas); scanf("%d",&n); init(); for(int i=1;i<=n;i++){ ll a;scanf("%lld",&a); for(int j=BASE;j>=0;j--){ if(a>>j&1){ if(b[j])a^=b[j]; else{ b[j]=a;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<=BASE;k++) if(b[k]>>j&1)b[k]^=b[j]; break; } } } } for(int i=0;i<=BASE;i++) if(b[i])mp.push_back(b[i]); int q; scanf("%d",&q); while(q--){ ll k; scanf("%lld",&k); printf("%lld\n",query(k)); } } return 0; }
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
原文地址:https://www.cnblogs.com/luyouqi233/p/8809826.html
时间: 2024-10-11 09:15:08