Mr Keks is a typical white-collar in Byteland.
He has a bookshelf in his office with some books on it, each book has an integer positive price.
Mr Keks defines the value of a shelf as the sum of books prices on it.
Miraculously, Mr Keks was promoted and now he is moving into a new office.
He learned that in the new office he will have not a single bookshelf, but exactly kk bookshelves. He decided that the beauty of the kk shelves is the bitwise AND of the values of all the shelves.
He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on kk shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.
The first line contains two integers nn and kk (1≤k≤n≤501≤k≤n≤50) — the number of books and the number of shelves in the new office.
The second line contains nn integers a1,a2,…ana1,a2,…an, (0<ai<2500<ai<250) — the prices of the books in the order they stand on the old shelf.
Print the maximum possible beauty of kk shelves in the new office.
10 4 9 14 28 1 7 13 15 29 2 31
24
7 3 3 14 15 92 65 35 89
64
题解:
从高位到低位考虑,若高位满足条件则先满足高位。
#include<bits/stdc++.h>
using namespace std;
const int maxn=55;
#define ll long long
ll a[maxn],sum[maxn];
bool dp[maxn][maxn];
int n,K;
bool check(ll x)//判断x是否满足条件
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=K;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=j;k++)
{
if((((sum[j]-sum[k-1])&x)==x)&&dp[i-1][k-1])//若满足条件则每个区间和&x都等于x
dp[i][j]=1;
}
}
}
return dp[K][n];
}
int main()
{
scanf("%d%d",&n,&K);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
ll ans=0;
for(ll i=62;i>=0;i--)
if(check(ans|(1LL<<i))) ans=ans|(1LL<<i);
printf("%lld\n",ans);
return 0;
}
(9+14+28+1+7)&(
本文介绍了一个关于书架分配的问题,需要将一定数量的书籍分配到多个书架上以最大化所有书架的美观度,美观度定义为各书架上书籍价格之和的按位与结果。文章提供了一种高效的解决方案,通过从高位到低位考虑来确定最优解。
720

被折叠的 条评论
为什么被折叠?



