codeforces1054F - Changing Array(思维)

本文探讨了F-ChangingArray问题,通过统计前缀异或和,寻找能够使区间异或和不为零的最大段落数。算法核心在于减少异或和为零的区间数量,通过对前缀异或结果进行统计与优化,实现最大化的非零异或区间数目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                       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;
}

 

内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值