LA4794 分享巧克力
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<=x, y<=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大小的巧克力分成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 分享巧克力的更多相关文章
- 来自PTA Basic Level的三只小野兽
点我阅读原文 最近利用闲暇时间做了一下 PTA Basic Level[1] 里的题,里面现在一共有 95 道题,这些题大部分很基础,对于刷倦了 leetcode 的小伙伴可以去里面愉快的玩耍哦. 这 ...
- 2019 年 Google 编程之夏活动报告
2019 年 Google 编程之夏活动报告 主要介绍了 GSoC 2019 活动的几个课题并讲述了整个活动的组织过程 Google 编程之夏活动不仅仅是一个夏日的实习项目,对于组织和一些社区的成员来 ...
- 2017年AR大会上海站干货分享
怀着即兴奋又激动的心情,踏上了第二次去上海的高铁,全长约1400公里行驶6小时15分钟,不算漫长的6个多小时里,对于不长出差的我来说,可谓是一种煎熬,再加上晕车的毛病,在去高铁的路上已经渐渐发作,但好 ...
- 【THUSC2017】巧克力
题目描述 “人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道.” 明明收到了一大块巧克力,里面有若干小块,排成n行m列.每一小块都有自己特别的图案ci,j,它们有的是海星,有的是贝壳,有的 ...
- 干货分享:常见的留学生Essay写作逻辑结构
任何一种类型的Essay写作都应遵循一种逻辑结构,Long Essay可能在不同部分依据情况使用不同的逻辑结构.本文将为大家分享六种常见留学生Essay写作逻辑结构,为方便阅读本文采用中英文对照方式. ...
- @uoj - 310@ 【UNR #2】黎明前的巧克力
目录 @description@ @solution@ @accepted code@ @details@ @description@ Evan 和 Lyra 都是聪明可爱的孩子,两年前,Evan 开 ...
- @loj - 2977@ 「THUSCH 2017」巧克力
目录 @description@ @solution@ @accepted code@ @details@ @description@ 「人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道.」 明 ...
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- Hangfire项目实践分享
Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget jobs) 延迟任务执行(De ...
随机推荐
- python的mp3play库试用
没有见过比这个更小型的库了,下面程序实现的功能:播放音乐,按空格键实现暂停和播放的切换. #coding=utf-8 import mp3play import pythoncom, pyHook i ...
- 黄聪:WordPress默认编辑器可视化切换不见了,非插件导致消失问题
1.后台---用户---我的个人资料 2.看看 [可视化编辑器]的[撰写文章时不使用可视化编辑器]项目是不是勾上了 3.去掉保存即可
- Android多线程断点下载的代码流程解析
Step 1:创建一个用来记录线程下载信息的表 创建数据库表,于是乎我们创建一个数据库的管理器类,继承SQLiteOpenHelper类 重写onCreate()与onUpgrade()方法 DBOp ...
- JavaScript Promise启示录--(转)
本博文转至:http://www.csdn.net/article/2014-05-28/2819979-JavaScript-Promise [编者按]JavaScript是一种基于对象和事件驱动并 ...
- 【UVA】11464 Even Parity(枚举子集)
题目 传送门:QWQ 分析 标准的套路题. 枚举第一行,接着根据第一行递推下面的行. 时间复杂度$ O(2^n \times n^2) $ 代码 #include <bits/stdc++.h& ...
- JAVA构造函数在超类和子类调用注意事项
1.构造函数: 当子类继承一个父类时,构造子类时需要调用父类的构造函数,存在三种情况 (1),父类无构造函数或者一个无参数构造函数,子类若无构造函数或者有无参数构造函数,子 ...
- Idea2016远程调试Java项目
一.在IDEA中配置远程tomcat 1.打开配置页面 选择"+" → "Tomcat Server" → "Remote" 填写名称和目标 ...
- OD 实验(三) - 破解程序的文件验证
需要破解的程序 双击程序,提示需要许可证文件 逆向程序 用 OD 打开 LoadIconA 为加载图标 LoadCursorA 为加载鼠标 F8 走一下程序 走到了这里,调用了 CreateFileA ...
- ChannelHandler,ChannelHandlerContext,ChannelPipeline
本小节一起学习一下ChannelHandler,ChannelHandlerContext,ChannelPipeline这三个Netty常用的组件,不探究它们的底层源码,我们就简单的分析一下用法 首 ...
- MySQL group_concat_max_len
MySQL提供的group_concat函数可以拼接某个字段值成字符串,如 select group_concat(user_name) from sys_user,默认的分隔符是 逗号,即" ...