前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 275 - To xor or not to xor

ACMSGURU 275 - To xor or not to xor

作者头像
Reck Zhang
发布2021-08-11 11:01:48
3370
发布2021-08-11 11:01:48
举报
文章被收录于专栏:Reck Zhang

To xor or not to xor

Problem Description

The sequence of non-negative integers A1, A2, …, AN is given. You are to find some subsequence Ai1, Ai2, …, Aik (1 <= i1 < i2 < … < ik <= N) such, that Ai1 XOR Ai2 XOR … XOR Aik has a maximum value.

Input

The first line of the input file contains the integer number N (1 <= N <= 100). The second line contains the sequence A1, A2, …, AN (0 <= Ai <= 10^18).

Output

Write to the output file a single integer number – the maximum possible value of Ai1 XOR Ai2 XOR … XOR Aik.

Sample test(s)

Input

3

11 9 5

Output

14

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    int n;
    std::cin >> n;

    auto nums = std::vector<long long>(n);
    for(auto& num : nums) {
        std::cin >> num;
    }

    long long res = 0;

    for(int index = 63; index >= 0; index--) {
        for(int i = 0; i < nums.size(); i++) {
            if(!(nums[i] & (1LL << index))) {
                continue;
            }
            if(!(res & (1LL << index))) {
                res ^= nums[i];
            }

            for(int j = 0; j < nums.size(); j++) {
                if((nums[j] & (1LL << index)) && j != i) {
                    nums[j] ^= nums[i];
                }
            }
            nums[i] = 0;
        }
    }

    std::cout << res << std::endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • To xor or not to xor
    • Problem Description
      • Input
        • Output
          • Sample test(s)
            • Input
            • Output
          • Solution
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档