Description
给定一个长度为n的序列a[1],a[2],…,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or … or c[m]。请求出总费用的最小值。
Input
第一行包含两个正整数n,m(1<=m<=n<=500000),分别表示序列的长度和需要划分的段数。
第一行包含n个整数,其中第i个数为ai。
Output
输出一个整数,即总费用的最小值。
Sample Input
3 2
1 5 7
Sample Output
3
HINT
第一段为[1],第二段为[5 7],总费用为(1) or (5 xor 7) = 1 or 2 = 3。
Source
By Claris
显然要让划分的异或和的高位尽量都为0
记录一下前缀异或和
然后枚举一个高位,如果存在一个高位使得至少m个异或和为0且总异或和(prev[n])为0,那么即可划分,此时这一位对答案的贡献为0,否则该位对答案的贡献为一个2的幂.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 500100
#define GET (ch >= '0' && ch <= '9')
#define LL long long
using namespace std;
int n,m,num;
LL a[MAXN];
LL ans;
bool used[MAXN];
void in(LL &x)
{
char ch = getchar();x = 0;
while (!GET) ch = getchar();
while (GET) x = x * 10 + ch - '0',ch = getchar();
}
int main()
{
scanf("%d%d",&n,&m);
for (int i = 1;i <= n;i++) in(a[i]),a[i] ^= a[i - 1];
for (LL j = 1ll << 62;j;j >>= 1)
{
num=0;
for (int i = 1;i <= n;i++)
if (!used[i] && (~a[i] & j)) num++;
if (num >= m && ~a[n]&j)
{
for (int i = 1;i <= n;i++)
if (a[i] & j) used[i] = 1;
}
else ans |= j;
}
cout<<ans<<endl;
}