AtCoder-Dwango Programming Contest V-B - Sum AND Subarrays

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/84670282

地址:https://dwacon5th-prelims.contest.atcoder.jp/tasks/dwacon5th_prelims_b

思路:贪心,首先将n*(n-1)/2个数求出来,在从高位开始遍历,若当前位为1的个数>=k,则将当前位为0的全部删除掉,同时ans加上当前位的值。

Code:

#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;

const int MAX_N=1005;
int n,m;
LL a[MAX_N];
vector<LL> v1,v2;

void Build();
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=0;i<n;++i)
		cin>>a[i];
	for(int i=0;i<n;++i)
	{
		LL ss=0;
		for(int j=i;j<n;++j)
		{
			ss+=a[j];
			v1.push_back(ss);
		}
	}
	LL ans=0;
	for(int i=40;i>=0;--i)
	{
		v2.clear();
		LL p=((LL)1<<i);
		for(auto c:v1)
			if(c&p)	v2.push_back(c);
		if(v2.size()>=m){
			ans+=p;
			v1.clear();	v1=v2;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/C_13579/article/details/84670282