Codeforces Contest 1101 problem G (Zero XOR Subset)-less —— 线性基

You are given an array a1,a2,…,an of integer numbers.

Your task is to divide the array into the maximum number of segments in such a way that:

each element is contained in exactly one segment;
each segment contains at least one element;
there doesn’t exist a non-empty subset of segments such that bitwise XOR of the numbers from them is equal to 0.
Print the maximum number of segments the array can be divided into. Print -1 if no suitable division exists.

Input
The first line contains a single integer n (1≤n≤2⋅105) — the size of the array.

The second line contains n integers a1,a2,…,an (0≤ai≤109).

Output
Print the maximum number of segments the array can be divided into while following the given constraints. Print -1 if no suitable division exists.

Examples
inputCopy
4
5 5 7 2
outputCopy
2
inputCopy
3
1 2 3
outputCopy
-1
inputCopy
3
3 1 10
outputCopy
3
Note
In the first example 2 is the maximum number. If you divide the array into {[5],[5,7,2]}, the XOR value of the subset of only the second segment is 5⊕7⊕2=0. {[5,5],[7,2]} has the value of the subset of only the first segment being 5⊕5=0. However, {[5,5,7],[2]} will lead to subsets {[5,5,7]} of XOR 7, {[2]} of XOR 2 and {[5,5,7],[2]} of XOR 5⊕5⊕7⊕2=5.

Let’s take a look at some division on 3 segments — {[5],[5,7],[2]}. It will produce subsets:

{[5]}, XOR 5;
{[5,7]}, XOR 2;
{[5],[5,7]}, XOR 7;
{[2]}, XOR 2;
{[5],[2]}, XOR 7;
{[5,7],[2]}, XOR 0;
{[5],[5,7],[2]}, XOR 5;
As you can see, subset {[5,7],[2]} has its XOR equal to 0, which is unacceptable. You can check that for other divisions of size 3 or 4, non-empty subset with 0 XOR always exists.

The second example has no suitable divisions.

The third example array can be divided into {[3],[1],[10]}. No subset of these segments has its XOR equal to 0.

题意:

给你n个数,让你分成尽可能多的集合,使得任意集合的异或不为0

题解:

第一次遇到线性基的题目,这道题就是求线性无关的秩
别人的模板

#include<bits/stdc++.h>
using namespace std;
int a[35];
int main()
{
    int n;
    scanf("%d",&n);
    int ans=0,ret=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        ret^=x;
        for(int j=31;j>=0;j--)
        {
            if((x&(1<<j)))
            {
                if(a[j])
                    x^=a[j];
                else
                {
                    a[j]=x,ans++;
                    break;
                }
            }
        }
    }
    printf("%d\n",ret==0?-1:ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值