• HDU 3949 XOR


    3949

    思路:

    线性基,线性基的每个元素尽可能小

    将k转换成二进制与排好序的线性基相对应

    如果线性基的个数小于n,说明n个元素线性相关,所以可以构成0,k要减1

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    #define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
    //head
    
    vector<LL> base;
    int main() {
        int T, n, q;
        LL t;
        scanf("%d", &T);
        for (int cs = 1; cs <= T; cs++) {
            scanf("%d", &n);
            base.clear();
            for (int i = 0; i < n; i++) {
                scanf("%lld", &t);
                for (int j = 0; j < base.size(); j++) t = min(t, t^base[j]);
                if(t) base.pb(t);
            }
            sort(base.begin(), base.end()); 
            int m = base.size();
            for (int i = 1; i < m; i++) {
                for (int j = i-1; j >= 0; j--) {
                    base[i] = min(base[i], base[i]^base[j]);
                }
            }
            scanf("%d", &q);
            printf("Case #%d:
    ", cs);
            while(q--) {
                scanf("%lld", &t);
                if(m < n) t--;
                if(t >= (1LL << m)) {
                    printf("-1
    ");
                    continue;
                }
                LL ans = 0;
                for (int j = 0; j < m; j++) if((t>>j)&1) ans ^= base[j];
                printf("%lld
    ", ans); 
            }
        }    
        return 0;
    } 
  • 相关阅读:
    1-4-04:奇偶ASCII值判断
    1-4-03:奇偶数判断
    1-4-02:输出绝对值
    1-4-01:判断数正负
    1-3-20:计算2的幂
    1-3-19:A*B问题
    1-3-18:计算三角形面积
    Use PIVOT Table in SQL Server
    Pivoting DataTable Simplified
    Pivot Methods 行列转换
  • 原文地址:https://www.cnblogs.com/widsom/p/9607656.html
Copyright © 2020-2023  润新知