codeforces1054F - Changing Array(思维)

                                       F - Changing Array

At a break Vanya came to the class and saw an array of nn kk-bit integers a1,a2,…,ana1,a2,…,anon the board. An integer xx is called a kk-bit integer if 0≤x≤2k−10≤x≤2k−1.

Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array ii (1≤i≤n1≤i≤n) and replace the number aiai with the number ai¯¯¯¯ai¯. We define x¯¯¯x¯ for a kk-bit integer xx as the kk-bit integer such that all its kk bits differ from the corresponding bits of xx.

Vanya does not like the number 00. Therefore, he likes such segments [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n) such that al⊕al+1⊕…⊕ar≠0al⊕al+1⊕…⊕ar≠0, where ⊕⊕ denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.

Input

The first line of the input contains two integers nn and kk (1≤n≤2000001≤n≤200000, 1≤k≤301≤k≤30).

The next line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2k−10≤ai≤2k−1), separated by spaces — the array of kk-bit integers.

Output

Print one integer — the maximum possible number of segments with XOR not equal to 00 that can be obtained by making several (possibly 00) operations described in the statement.

Examples

Input

3 2
1 3 0

Output

5

Input

6 3
1 4 4 7 3 4

Output

19

Note

In the first example if Vasya does not perform any operations, he gets an array that has 55 segments that Vanya likes. If he performs the operation with i=2i=2, he gets an array [1,0,0][1,0,0], because 3¯¯¯=03¯=0 when k=2k=2. This array has 33 segments that Vanya likes. Also, to get an array with 55 segments that Vanya likes, he can perform two operations with i=3i=3 and with i=2i=2. He then gets an array [1,0,3][1,0,3]. It can be proven that he can't obtain 66 or more segments that he likes.

In the second example, to get 1919 segments that Vanya likes, he can perform 44operations with i=3i=3, i=4i=4, i=5i=5, i=6i=6 and get an array [1,4,3,0,4,3][1,4,3,0,4,3].

 

一、原题地址

点我传送

 

二、大致题意

给出n个数字,他们可以被替换为自身按位取反的数,给出区间[ l , r ]询问在这段区间内异或和不为零的区间的个数。

 

三、思路

总区间的个数就是n*(n+1)/2。只需要找出那些区间异或是0的区间个数减去就行。

统计前缀异或,对于前缀异或来说,如果在a,b位置有两个相同的前缀异或和出现,则表示这两个位置a,b上会存在一个为零的区间。如果在a,b,c上有相同的异或和,则存在3个为零的区间。如果在abcd上出现相同的异或和,那么存在6个为零的区间。以此类推。

那么首先统计一遍前缀和中会出现的数,对于一个位置上的前缀异或和来说,一定只有两种可能,要么是本身,要么是按位取反。

得出的结果数量保存在一个容器里,最后要使得为零的区间最少,那么自然就是对半拆开了。

 四、代码

#include <iostream>
#include<string.h>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const LL inf=0x3f3f3f3f;
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }


const int maxn=200005;
LL n,k;
LL sum[maxn];
map<LL,LL>num;
int main()
{
    scanf("%lld %lld",&n,&k);
    LL limt=(1<<k)-1;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&sum[i]);
        sum[i]=sum[i]^sum[i-1];
        num[min(sum[i],sum[i]^limt)]++;
    }
    num[0]++;
    LL ans=n*(n+1)/2;
    for(map<LL,LL>::iterator it=num.begin();it!=num.end();it++)
    {
        LL x=it->second;
        LL d1=x/2;
        LL d2=x-d1;
        ans-=d1*(d1-1)/2;
        ans-=d2*(d2-1)/2;
    }
    cout<<ans<<endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值