UVALive4794 Sharing Chocolate

摘要

状压DP

链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2795

题解

这题有个很好的特性,它要求每块地和加起来必须恰好等于整块的面积
所以 Seach part=Sall ∑ S e a c h   p a r t = S a l l ,也就是长乘宽等于每一块的面积之和,
设计状态 f[x][S] f [ x ] [ S ] 其中, x x 是长和宽中较小的数,S是一个二进制数,代表了一个集合,第 i i 位为1表示第i ai a i 被选中,因为 xy=Area(S) x y = A r e a ( S ) ,所以知道了 S S x就知道了 y y
初始状态也就是S中只有一个元素,且 x x Area(S)的约数,这时的 f[x][S]=true f [ x ] [ S ] = t r u e
转移也有技巧
因为是从中间劈成两个,
先考虑横着劈开
可以先枚举 S S 的真子集s,然后 s,Ss s , S − s 的面积如果都是 y y 的倍数,就
f[x][S]f[min(y,Area(S)/y)][s] and f[min(y,Area(Ss)/y)][Ss]
其中 and a n d 是逻辑与
竖着劈同理
时间复杂度 O(min(x,y)3n) O ( m i n ( x , y ) 3 n )
直接写会 T T 掉,记忆化搜索500ms即可过

代码

//状压DP
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
int a[20], sum[(1<<15)+10], n, X, Y;
bool vis[110][(1<<15)+10], f[110][(1<<15)+10];
int read(int x=0)
{
    char c, f=1;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-48;
    return x*f;
}
bool init()
{
    int i, j;
    cl(sum), cl(vis), cl(f);
    X=read(), Y=read();
    for(i=0;i<n;i++)a[i]=read();
    for(i=0;i<1<<n;i++)
        for(j=0;j<n;j++)if(i&(1<<j))sum[i]+=a[j];
    if(sum[(1<<n)-1]!=X*Y)return 0;
    return 1;
}
bool bitcount(int x)
{
    for(;(x&1)==0;x>>=1);
    return x==1;
}
int dp(int x, int S)        //记忆化搜索 
{
    int s, i, y=sum[S]/x;
    if(x>y)swap(x,y);
    if(vis[x][S])return f[x][S];
    vis[x][S]=1;
    if(bitcount(S))return f[x][S]=1;
    for(s=(S-1)&S;s and !f[x][S];s=(s-1)&S)
    {
        if(sum[s]%y==0 and sum[S^s]%y==0)f[x][S]=dp(y,s) and dp(y,S^s);
        if(!f[x][S] and sum[s]%x==0 and sum[S^s]%x==0)f[x][S]=dp(x,s) and dp(x,S^s);
    }
    return f[x][S];
}
//int gcd(int a, int b){return !b?a:gcd(b,a%b);}
//int dp()      //直接DP 
//{
//  int S, s, x, g;
//  for(S=1;S<1<<n;S<<=1)
//      for(x=1;x*x<=sum[S];x++)if(sum[S]%x==0)f[x][S]=1;
//  for(S=1;S<1<<n;S++)for(s=(S-1)&S;s;s=(s-1)&S)
//  {
//      g=gcd(sum[s],sum[S^s]);
//      for(x=1;x<=g and x*x<=sum[S] and x<=min(X,Y);x++)
//      {
//          if(g%x==0)f[x][S]|=f[min(x,sum[s]/x)][s] and f[min(x,sum[S^s]/x)][S^s];
//          if(g%(sum[S]/x)==0)f[x][S]|=f[min(sum[S]/x,sum[s]/(sum[S]/x))][s] and f[min(sum[S]/x,sum[S^s]/(sum[S]/x))][S^s];
//      }
//  }
//  return f[min(X,Y)][(1<<n)-1];
//}
int main()
{
    int c;
    for(c=1,n=read();n;n=read(),c++)
    {
        printf("Case %d: ",c);
        if(init())
        {
            if(dp(X,(1<<n)-1))printf("Yes\n");
            else printf("No\n");
        }
        else printf("No\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值