贪心的按位考虑。如果所有数在某一位上有奇数个为1,显然无论如何划分这一位最终都会为1;否则将每一部分都划分为偶数个1就能保证最终该位为0,可以标记上哪些位置可以作为划分点(当然也要满足之前可为0的位上是0),如果剩余划分点个数>=m-1则说明该位可为0。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define N 500010 #define ll long long ll read() { ll x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,cnt; ll a[N],ans; bool flag[N]; int main() { #ifndef ONLINE_JUDGE freopen("bzoj4245.in","r",stdin); freopen("bzoj4245.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif cnt=n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int k=62;~k;k--) { int t=0; for (int i=1;i<=n;i++) t+=(a[i]&(1ll<<k))>0; if (t&1) ans+=1ll<<k; else { t=0; for (int i=1;i<n;i++) { t+=(a[i]&(1ll<<k))>0; if (t&1) cnt-=1-flag[i],flag[i]=1; } if (cnt<m) ans+=1ll<<k; } } cout<<ans; return 0; }