uva1099 - Sharing Chocolate 切巧克力

Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world.

You find that the only thing better than eating chocolate is to share it with friends. Unfortunately your friends are very picky and have different appetites: some would like more and others less of the chocolate that you offer them. You have found it increasingly difficult to determine whether their demands can be met. It is time to writte a program that solves the problem once and for all!


Your chocolate comes as a rectangular bar. The bar consists of same-sized rectangular pieces. To share the chocolate you may break one bar into two pieces along a division between rows or columns of the bar. You or the may then repeatedly break the resulting pieces in the same manner. Each of your friends insists on a getting a single rectangular portion of the chocolate that has a specified number of pieces. You are a little bit insistent as well: you will break up your bar only if all of it can be distributed to your friends, with none left over.


For exampla, Figure 9 shows one way that a chocolate bar consisting of 3 x 4 pieces can be split into 4 parts that contain 6, 3, 2, and 1 pieces respectively, by breanking it 3 times (This corresponds to the first sample input.)

Input 

The input consists of multiple test cases each describing a chocolate bar to share. Each description starts with a line containing a single integer n (1$ \le$n$ \le$15), the number of parts in which the bar is supposed to be split. This is followed by a line containing two integers x and y (1$ \le$x, y$ \le$100), the dimensions of the chocolate bar. The next line contains n positive integers, giving the number of pieces that are supposed to be in each of the n parts.

The input is terminated by a line containing the integer zero.

Output 

For each test case, first display its case number. Then display whether it is possible to break the chocolate in the desired way: display ``Yes" if it is possible, and ``No" otherwise. Follow the format of the sample output.

Sample Input 

4 
3 4 
6 3 2 1 
2 
2 3 
1 5 
0

Sample Output 

Case 1: Yes 
Case 2: No

  给X*Y的巧克力,只能横着或竖着切,问能不能正好切成面积分别为ai的N块。

  一个很重要的优化是看要得到的总面积是不是等于要切的面积,是不是要切的变长的整数倍。用sum[i]保存集合i巧克力的总面积。用记忆化搜索模拟每一刀切哪,遍历当前集合S的子集。设x<y,若顺着x方向切,子集S0的面积是边长x的整数倍才有可能,这时就切成一块边长x,sum[S0]/x的一块,剩下的就是另一块,如果这一块能刚好切成S0,另一块能刚好切成S0在S中的补集S^S0,就符合题意。同样枚举顺着y方向切的情况。若当前集合只剩1块,return 1。


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
#define MAXN 16
#define MAXM 1010
#define eps 1e-9
#define pi 4*atan(1.0)
#define pii pair<int,int>
using namespace std;
int N,X,Y;
int d[1<<MAXN][110],vis[1<<MAXN][110],sum[1<<MAXN],a[MAXN];
int bitcount(int x){
    int ret=0;
    while(x){
        if(x&1) ret++;
        x>>=1;
    }
    return ret;
}
int DP(int S,int x){
    int &ans=d[S][x];
    if(vis[S][x]) return ans;
    vis[S][x]=1;
    if(bitcount(S)==1) return ans=1;
    int y=sum[S]/x;
    for(int S0=S;S0;S0=S&(S0-1)){
        int S1=S^S0;
        if(sum[S0]%x==0&&DP(S0,min(x,sum[S0]/x))&&DP(S1,min(x,sum[S1]/x))) return ans=1;
        if(sum[S0]%y==0&&DP(S0,min(y,sum[S0]/y))&&DP(S1,min(y,sum[S1]/y))) return ans=1;
    }
    return ans=0;
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N),N){
        scanf("%d%d",&X,&Y);
        for(int i=0;i<N;i++) scanf("%d",&a[i]);
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        for(int i=0;i<(1<<N);i++)
            for(int j=0;j<N;j++) if(i&(1<<j)) sum[i]+=a[j];
        int all=(1<<N)-1;
        printf("Case %d: ",++cas);
        if(sum[all]!=X*Y||sum[all]%X) printf("No\n");
        else if(DP(all,min(X,Y))) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值