CF981D Bookshelves

传送门

这居然是个*1800的题,有点无法接受,可能自己\(dp\)方面确实差了点
考虑按位\(dp\),从高位到低位枚举,然后用\(dp\)去判断是否可行。
然后设\(f[i][j]\)表示前\(i\)个分成\(j\)块能否满足当前枚举的答案
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=60;
int n,k,f[maxn][maxn];long long a[maxn],ans;
bool dp(long long ans)
{
    memset(f,0,sizeof f);f[0][0]=1;
    for(rg int i=1;i<=n;i++)
        for(rg int j=0;j<i;j++)
            for(rg int k=j;k<i;k++)
                if(((a[i]-a[k])&ans)==ans)f[i][j+1]|=f[k][j];
    return f[n][k];
}
int main()
{
    read(n),read(k);
    for(rg int i=1;i<=n;i++)scanf("%lld",&a[i]),a[i]+=a[i-1];
    for(rg int i=60;i>=0;i--)if(dp(ans|(1ll<<i)))ans|=1ll<<i;
    printf("%lld\n",ans);
}

转载于:https://www.cnblogs.com/lcxer/p/10639236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值