持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第23天,点击查看活动详情
本文已参与「新人创作礼」活动,一 起开启掘金创作之路\
题目
At a break Vanya came to the class and saw an array of -bit integers on the board. An integer is called a -bit integer if . 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 () and replace the number with the number . We define for a -bit integer as the -bit integer such that all its bits differ from the corresponding bits of . Vanya does not like the number . Therefore, he likes such segments () such that , where denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.
中文大意
给我们一个数字和一个数字表示数组中的元素个数k表示每个的二进制位数不超过我们可以任意对元素操作让二进制k位上的数都不同问有多少区间
解法
根据鸽巢原理我们可以知道如果再一段连续的区间异或和中出现了两个相同的值那么就可以确定在这两点之间的区间的异或和是为0的所以本题跟着这个思路来写。
然后我们利用贪心思想从左往右每次让这个区间中连续异或和不为0的数量最大即可
int a[N];
void solve()
{
int n,k; cin >> n >> k;
k = 1 << k;
k -= 1;
rep(i,n) cin >> a[i];
map<int,int> mp;
mp[0] = 1;
int res = 0;
for(int i = 1; i <= n; i++) {
a[i] ^= a[i-1];
if(mp[a[i]] >= mp[a[i] ^ k]) a[i] ^= k;
res += mp[a[i]] ++;
}
cout << n * (n + 1) / 2 - res << endl;
}