zoukankan      html  css  js  c++  java
  • BZOJ 4269: 再见Xor

    4269: 再见Xor

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 267  Solved: 161
    [Submit][Status][Discuss]

    Description

    给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值。

    Input

    第一行一个正整数N。
    接下来一行N个非负整数。

    Output

    一行,包含两个数,最大值和次大值。

    Sample Input

    3
    3 5 6

    Sample Output

    6 5

    HINT

    100% : N <= 100000, 保证N个数不全是0,而且在int范围内 

    Source

    题解:

    线性基板子题...

    异或空间内的最大值就是线性基的异或和,次大值就是去掉线性基内最小的数之后的异或和...

    代码:

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    //by NeighThorn
    using namespace std;
     
    const int maxn=100000+5;
     
    int n,ans,cnt,a[maxn];
     
    inline void xor_gauss(void){
        for(int i=1;i<=n;i++){
            for(int j=n;j>i;j--)
                if(a[i]<a[j])
                    swap(a[i],a[j]);
            if(a[i])
                cnt++;
            else
                break;
            for(int j=31;~j;j--)
                if((a[i]>>j)&1){
                    for(int k=1;k<=n;k++)
                        if(k!=i&&a[k]&&(a[k]>>j)&1)
                            a[k]^=a[i];
                    break;
                }
        }
    }
     
    signed main(void){
        scanf("%d",&n);cnt=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        ans=0;xor_gauss();
        for(int i=1;i<=cnt;i++)
            ans^=a[i];
        printf("%d %d
    ",ans,ans^a[cnt]);
        return 0;
    }
    

      


    By NeighThorn

  • 相关阅读:
    Pentaho
    sympy 解四元一次方程
    install R language on ubuntu
    pyside
    浙江省医院网上挂号
    mtu值相关
    Python 中除法运算需要注意的几点
    idea
    kilim
    good blog
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6390431.html
Copyright © 2011-2022 走看看