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. JDBC--数据库链接及相关方法的封装

    使用的是MySQL数据库,首先导入驱动类,然后根据数据库URL和用户名密码获得数据的链接.由于使用的是MySQL数据库,它的URL一般为,jdbc:mysql://主机地址:端口号/库名. 下面是封装 ...

  2. ActiveMQ入门之四--ActiveMQ持久化方式

    消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...

  3. NFV网络功能虚拟化 基本概念

    NFV基本概念 NFV则由运营商联盟提出,为了加速部署新的网络服务,运营商倾向于放弃笨重昂贵的专用网络设备,转而使用标准的IT虚拟化技术来拆分网络功能模块,如DNS.NAT.Firewall等.于是一 ...

  4. python学习(二十六) 字典

    # 访问字典car = {'make':'bmw', 'model':'550i', 'year':2016}print(car)print(car['make']) # 添加元素d = {}d['o ...

  5. Java复习——多线程与并发库

    开启一个线程 实现一个线程的方式有两种:继承Thread类.实现Runnable接口(也存在说三种的情况,第三种是使用线程并发库中的线程池创建一个线程).这两种方法都需要重写Run方法,具体的线程逻辑 ...

  6. Spring中引质增强的安全

    在引质增强中使用ThreadLocal变量,是因为控制状态使代理类变成了非线程安全的实例,为了解决单线程安全的问题,通过ThreadLocal让每个线程单独使用一个状态.

  7. django-bower

    https://django-bower.readthedocs.io/en/latest/usage.html

  8. 第四章:重构代码[学习Android Studio汉化教程]

    第四章 Refactoring Code The solutions you develop in Android Studio will not always follow a straight p ...

  9. C语言学习笔记---好用的函数memcpy与memset

    这个主要用于我个人的学习笔记,便于以后查询,顺便分享给大家. 想必在用C的时候难免会与数组,指针,内存这几样东西打交道,先以数组为例,例如有一个数组int a[5] = {1, 2, 3, 4, 5} ...

  10. leetcode289

    public class Solution { public void GameOfLife(int[][] board) { ) - ; ].GetLength() - ; var list = n ...