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<=n<=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<=xy<=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


3 4 
6 3 2 1 

2 3 
1 5 0

Sample Output

Case 1: Yes
Case 2: No 题意: 将一个x*y大小的巧克力分成n分, 固定大小a[i], 判断是否可以成功. 解题思路:
1. 设状态dp[x][y][S]: x*y的巧克力是否可以分割成集合S.
2. dp[x][y][S]=true 当且仅当:
  dp[x0][y][S0] = dp[x-x0][y][S-S0] = true //1<=x0<x,S0是S的子集
OR dp[x][y0][S0] = dp[x][y-y0][S-S0] = true //1<=y0<y,S0是S的子集
3.把集合S中面积的总和记为sum[S]
4.只计算sum(S)=x*y的
5.dp[x][y][S] = dp[y][x][S],不必重复计算
我们设x<y,用dp[x][S]代替dp[x][y][S]和dp[y][x][S]
此时x为较小的边长,y为sum(S)/x.
方程变化为:
dp[x][S]=true 当且仅当:   
    sum[S0]%x==0 AND dp[min(x,sum[S]/x)][S0] = dp[min(x,sum[S]/x)][S-S0] = true
或者是 sum[S0]%y==0 AND dp[min(y,sum[S]/y)][S0] = dp[min(y,sum[S]/y)][S-S0] = true
6.输入之后要比较所有所有a[i]的和是否等于x*y

代码如下:
#include<iostream>
#include<cstring>
#define Max_n 15
#define Size 105
using namespace std; bool d[Size][(<<Max_n)];
bool vis[Size][(<<Max_n)];
int sum[(<<Max_n)];
int a[Max_n];
int n,x,y; int bitcount(int S){
int num=;
for(int i=;i<n;i++)
if(S&(<<i))num++;
return num;
} bool dp(int x,int S){
if(vis[x][S])return d[x][S]; vis[x][S]=true;
bool& ans=d[x][S]; if(bitcount(S)==)return ans=true; int y=sum[S]/x;
for(int S0=(S-)&S;S0;S0=(S0-)&S){
int S1=S-S0;
if(sum[S0]%x== && dp(min(x,sum[S0]/x),S0) && dp(min(x,sum[S1]/x),S1)) return ans=true;
if(sum[S0]%y== && dp(min(y,sum[S0]/y),S0) && dp(min(y,sum[S1]/y),S1)) return ans=true;
} return ans=false;
} int main(){
freopen("32.in","r",stdin); while(scanf("%d",&n)== && n!=){
memset(vis,false,sizeof(vis)); cin>>x>>y;
for(int i=;i<n;i++){
cin>>a[i];
} for(int S=;S<(<<n);S++){
for(int j=;j<n;j++){
if(S&(<<j)){
sum[S]+=a[j];
}
}
} int ALL=(<<n)-;
int ans;
if(sum[ALL]!=x*y)ans=;
else ans=dp(min(x,y),ALL); cout<<(ans?"Yes":"No")<<endl;
} fclose(stdin);
return ;
}

LA4794 分享巧克力的更多相关文章

  1. 来自PTA Basic Level的三只小野兽

    点我阅读原文 最近利用闲暇时间做了一下 PTA Basic Level[1] 里的题,里面现在一共有 95 道题,这些题大部分很基础,对于刷倦了 leetcode 的小伙伴可以去里面愉快的玩耍哦. 这 ...

  2. 2019 年 Google 编程之夏活动报告

    2019 年 Google 编程之夏活动报告 主要介绍了 GSoC 2019 活动的几个课题并讲述了整个活动的组织过程 Google 编程之夏活动不仅仅是一个夏日的实习项目,对于组织和一些社区的成员来 ...

  3. 2017年AR大会上海站干货分享

    怀着即兴奋又激动的心情,踏上了第二次去上海的高铁,全长约1400公里行驶6小时15分钟,不算漫长的6个多小时里,对于不长出差的我来说,可谓是一种煎熬,再加上晕车的毛病,在去高铁的路上已经渐渐发作,但好 ...

  4. 【THUSC2017】巧克力

    题目描述 ​“人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道.” ​ 明明收到了一大块巧克力,里面有若干小块,排成n行m列.每一小块都有自己特别的图案ci,j,它们有的是海星,有的是贝壳,有的 ...

  5. 干货分享:常见的留学生Essay写作逻辑结构

    任何一种类型的Essay写作都应遵循一种逻辑结构,Long Essay可能在不同部分依据情况使用不同的逻辑结构.本文将为大家分享六种常见留学生Essay写作逻辑结构,为方便阅读本文采用中英文对照方式. ...

  6. @uoj - 310@ 【UNR #2】黎明前的巧克力

    目录 @description@ @solution@ @accepted code@ @details@ @description@ Evan 和 Lyra 都是聪明可爱的孩子,两年前,Evan 开 ...

  7. @loj - 2977@ 「THUSCH 2017」巧克力

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 「人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道.」 明 ...

  8. .net点选验证码实现思路分享

    哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...

  9. Hangfire项目实践分享

    Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget jobs) 延迟任务执行(De ...

随机推荐

  1. 双口RAM,值得研究

    在FPGA设计过程中,使用好双口RAM,也是提高效率的一种方法. 官方将双口RAM分为简单双口RAM和真双口RAM. 简单双口RAM只有一个写端口,一个读端口. 真双口RAM分别有两个写端口和两个读端 ...

  2. POJ 1222 EXTENDED LIGHTS OUT(反转)

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12616   Accepted: 8 ...

  3. python学习 (二十八) Python的for 循环

    1: for 循环可以循环如下类型: my_string = "abcabc" // 字符串类型 for c in my_string: print(c, end=' ') car ...

  4. ubuntu16.04编译QT5.6所依赖的库

    首先在QT的根目录下,阅读README文件! 里面介绍了ubuntu环境下,编译该版本的QT需要安装的包 New dependencies in Qt 5    ------------------- ...

  5. Web页面工作流设计器

    http://www.cnblogs.com/2018/archive/2011/11/22/2240259.html http://wenku.baidu.com/link?url=LSqlCiqi ...

  6. 检测python进程是否存活

    crontab -e */ * * * * /data/log_realtime/check.sh > /data/log_realtime/check.log >& * * /d ...

  7. 【汇总】C#数据类型及转换

    13.C# 16进制与字符串.字节数组之间的转换 https://www.cnblogs.com/seventeen/archive/2009/10/29/1591936.html C# 对象.文件与 ...

  8. hibernate操作数据库时报错解决方式

    java.sql.SQLException: Parameter index out of range (28 > number of parameters, which is 27). 这个说 ...

  9. linux find中的-print0和xargs中-0的奥妙

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...

  10. CentOS7.6安装Nodejs(Npm)

    官网下载地址:https://nodejs.org/en/download/ 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进制包:wg ...