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
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值