zoukankan      html  css  js  c++  java
  • HDU3949 XOR

    线性基模板

    线性基可以看成把一组序列处理过后得到的新数组,他和原序列异或和的值域完全相同,也就是说原序列的任意几个数的异或和都可以被线性基的数表示出来,因此线性基可以看成数原序列的替代。

    我们通过特殊的方法处理出线性基,可以快速求出原序列中异或和最大的子集,以及第k小的子集。

    存一下自己的板子

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long LL;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int ret = 0, w = 0; char ch = 0;
        while(!isdigit(ch)){
            w |= ch == '-', ch = getchar();
        }
        while(isdigit(ch)){
            ret = (ret << 3) + (ret << 1) + (ch ^ 48);
            ch = getchar();
        }
        return w ? -ret : ret;
    }
    inline int lcm(int a, int b){ return a / __gcd(a, b) * b; }
    template <typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    
    const int N = 20005;
    int _, n, m, cnt, k, cs;
    LL val, b[N], a[N];
    void insert(LL x){
        for(int i = 62; i >= 0; i --){
            if(x & (1LL << i)){
                if(!b[i]){
                    b[i] = x;
                    break;
                }
                else x ^= b[i];
            }
        }
    }
    
    LL maximum(){
        LL ret = 0;
        for(int i = 62; i >= 0; i --){
            if((ret ^ b[i]) > ret) ret ^= b[i];
        }
        return ret;
    }
    
    LL minimum(){
        for(int i = 0; i<= 62; i ++){
            if(b[i]) return b[i];
        }
    }
    
    
    void rebuild(){
        full(a, 0), cnt = 0;
        for(int i = 62; i >= 0; i --){
            for(int j = i - 1; j >= 0; j --)
                if(b[i] & (1LL << j)) b[i] ^= b[j];
        }
        for(int i = 0; i <= 62; i ++){
            if(b[i]) a[cnt++] = b[i];
        }
    }
    
    LL query(int k){
        if(k >= (1LL << cnt)) return -1;
        LL ret = 0;
        for(int i = 62; i >= 0; i --){
            if(k & (1LL << i)) ret ^= a[i];
        }
        return ret;
    }
    
    
    int main(){
    
        for(scanf("%d", &_); _; _ --){
            full(b, 0);
            scanf("%d", &n);
            for(int i = 1; i <= n; i ++){
                scanf("%lld", &val);
                insert(val);
            }
            rebuild();
            scanf("%d", &m);
            printf("Case #%d:
    ", ++cs);
            while(m --){
                scanf("%d", &k);
                if(n != cnt) k --;
                printf("%lld
    ", query(k));
            }
        }
        return 0;
    }
    
  • 相关阅读:
    PS教程1000例
    [LeetCode] Ransom Note 赎金条
    Android LinkedList和ArrayList的区别
    Android 一种非常好用的Android屏幕适配
    Android MVC模式和MVP模式的区别
    Android 性能优化的方面方面都在这儿
    Android 高级面试题及答案
    Android的事件分发(dispatchTouchEvent),拦截(onInterceptTouchEvent)与处理(onTouchEvent)
    android studio 3.0 以上 查看sharedpreference
    Android adb命令查看sharedpreferences
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/11230807.html
Copyright © 2011-2022 走看看