码迷,mamicode.com
首页 > 其他好文 > 详细

E 牛牛数数 线性基+二分

时间:2021-01-26 12:07:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:mic   rgb   splay   c++   out   names   ++   inf   fine   

技术图片

 

 

技术图片
  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 #define ENDL "\n"
  5 typedef long long ll;
  6 typedef pair<int, int> pii;
  7 const int inf = 0x7fffffff;
  8 const int maxn = 2e5 + 10;
  9 
 10 struct LB {
 11     ll a[105];
 12     bool isZero;
 13     int maxlen;
 14     vector<ll> all;
 15 
 16     void init(int len) {
 17         maxlen = len, isZero = 0;
 18         memset(a, 0, sizeof a);
 19     }
 20 
 21     void build(int n, ll *b) {
 22         for (int i = 1; i <= n; i++) {
 23             if (!insert(b[i])) isZero = 1;
 24         }
 25         all.clear();
 26         for (int i = 0; i <= maxlen; i++) {
 27             if (a[i]) all.push_back(a[i]);
 28         }
 29     }
 30 
 31     ll queryMax() {
 32         ll ans = 0;
 33         for (int i = 0; i <= maxlen; i++) {
 34             ans ^= a[i];
 35         }
 36         return ans;
 37     }
 38 
 39     ll queryMin() {
 40         if (isZero) return 0;
 41         for (int i = 0; i <= maxlen; i++) {
 42             if (a[i]) return a[i];
 43         }
 44         return 0;
 45     }
 46 
 47     bool ask(ll x) {
 48         for (int i = maxlen; i >= 0; i--) {
 49             if (x & (1LL << i)) x ^= a[i];
 50         }
 51         return x == 0;
 52     }
 53     bool insert(ll x) {
 54         for (int i = maxlen; i >= 0; i--) {
 55             if (!(x & (1LL << i))) continue;
 56             if (a[i])
 57                 x ^= a[i];
 58             else {
 59                 for (int j = 0; j < i; j++) {
 60                     if (x & (1LL << j)) x ^= a[j];
 61                 }
 62                 for (int j = i + 1; j <= maxlen; j++) {
 63                     if (a[j] & (1LL << i)) a[j] ^= x;
 64                 }
 65                 a[i] = x;
 66                 return 1;
 67             }
 68         }
 69         return 0;
 70     }
 71     ll queryKth(ll k) {
 72         if (isZero) k--;
 73         int cnt = all.size();
 74         if (k >= (1LL << cnt)) return -1;
 75         ll ans = 0;
 76         for (int i = 0; i < all.size(); i++) {
 77             if (k & (1LL << i)) ans ^= all[i];
 78         }
 79         return ans;
 80     }
 81 } q;
 82 ll b[maxn];
 83 int main() {
 84     // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 85     int n;
 86     ll k;
 87     cin >> n >> k;
 88     for (int i = 1; i <= n; i++) {
 89         cin >> b[i];
 90     }
 91     q.init(61);
 92     q.build(n, b);
 93     ll l = 1, r = (1LL << (int)q.all.size()) + q.isZero; //r为实际的上界加一
 94     while (l < r) {
 95         ll mid = (l + r) >> 1;
 96         if (q.queryKth(mid) > k)
 97             r = mid;
 98         else
 99             l = mid + 1;
100     }
101     r = (1LL << (int)q.all.size()) - 1 + q.isZero;
102     cout << r - l + 1 << endl;
103     return 0;
104 }
View Code

 

E 牛牛数数 线性基+二分

标签:mic   rgb   splay   c++   out   names   ++   inf   fine   

原文地址:https://www.cnblogs.com/pangbi/p/14322034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!