HDU-3949-线性基

题目
线性基是学线性代数上出现的概念,没想到那么有用。把一个集合弄成一个极大线性无关子集,就能把他所能表示的数都能存起来。求第k小我也不会证。。就当先存个板子吧

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (int i = (n); i < (m); i++)
#define _rep(n,m,i) for (int i = (n); i <= (m); i++)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
struct Liner_Basis{
  LL d[63], f[63], tot;
  bool flag;
  Liner_Basis() {memset(d, 0, sizeof(d)), memset(f, 0, sizeof(f));tot = 0;flag = false; }
  void insert(LL x) {
    for(int i = 62; i >= 0; i--) {
      if((x >> i )& 1) {
        if(!d[i]) {
          d[i] = x;
          return ;
        }
        x ^= d[i];
      }
    }
    flag = true;
  }
  void rebuild() {
    for(int i = 62; i >= 0; i--) {
      for(int j = i-1; j >= 0; j--) {
        if(d[i] >> j & 1) d[i] ^= d[j];
      }
    }
    for(int i = 0; i <= 62; i++) if(d[i]) f[tot++] = d[i];
    return ;
  }
  LL k_th(LL k) {
    if(flag) k--;
    if(k == 0) return 0;
    LL num = 0;
    if(k >= (1ll << tot)) return -1;
    for(int i = 62; i >= 0; i--) {
      if(k >> i & 1) num ^= f[i];
    }
    return num;
  }
};
int main() {
  fio
  int t;
  cin >> t;
  for(int ci = 1; ci <= t; ci++) {
    int n;LL x;Liner_Basis LB;
    cin >> n;
    while(n--) {
      cin >> x;
      LB.insert(x);
    }
    LB.rebuild();
    cin >> n;
    cout << "Case #" << ci << ":\n";
    while(n--) {
      cin >> x;
      cout << LB.k_th(x) << endl;
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值