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 ...
随机推荐
- 常见企业IT支撑【8、端口回流问题】
端口回流故障场景,常见于内网启用了Server服务器,使用出口路由设备的外网口NAT映射了一个公网地址,域内内网主机访问了这个公网地址,访问不通. 故障容易出现在办公网内的带有对外Server测试环境 ...
- json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 column 33 (char 33)
json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 colu ...
- 【Leetcode 136】Single Number
问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...
- redis数据结构对象
redis的数据结构对象包括 字符串 列表 哈希 集合 有序集合五种数据结构对象,由底层的8种数据结构组成这五种对象,每种对象的实现不同的数据都是不一样的. 结构 typedef struct red ...
- Cousera 无法播放视频 解决办法 widows 和 linux
查资料得知,Cousera无法播放课程视频原因在于DNS污染. 尽管通过FQ软件把视频看完了,在最后一课找到了这个解决办法,现在拿出来分享给大家: Windows: 请参照以下链接: http://j ...
- znpc改版前后网址修改办法
znpc改版前后网址修改办法把原网址中的http://bbs.znpc.net/viewthread.php?替换为http://bbs.znpc.net/forum.php?mod=viewthre ...
- ThreadPoolExecutor之三:自定义线程池-扩展示例
ThreadPoolExecutor是可扩展的,下面一个示例: package com.dxz.threadpool.demo1; import java.util.concurrent.Blocki ...
- AbstractQueuedSynchronizer原理分析
AbstractQueuedSynchronized 以下简称AQS,是用来构建锁或者其他同步组件的基础框架. 在AQS中,为锁的获取和释放提供了一些模板方法,而实现锁的类(AQS的子类)需要实现这些 ...
- nginx 反向代理与负载均衡应用实践
集群介绍 集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器.这些服务器之间可以彼此通信,协同向 ...
- Go - 开始
学习Go的缘由 由于LZ目前在使用docker,docker的编程语言使用的是“Go”,所以想更加深入的了解Docker(尝试着看懂source code)遂尝试了解下Golang. 安装 LZ用的是 ...