这道题目的DP思想挺先进的,用状态DP来表示各个子巧克力块。原本是要 dp(S,x,y),S代表状态,x,y为边长,由于y可以用面积/x表示出来,就压缩到了只有两个变量,在转移过程也是很巧妙,枚举S的子集s0,然后 s1=S-s0来代表除该子集的另一个集合,接下来分两种情况,如果这个子集是通过把 S保留x,切割y,则转移到dp(s0,x)和dp(s1,x),另一种情况是转移到dp(s0,y)和dp(s1,y)。为了更加缩小状态,统一把转移方程的 x换成 min(x,sum[S]/x),sum为该状态下的面积

#include <cstdio>
#include <cstring>
#include <algorithm>
//#include <cmath>
#define N 1<<17
using namespace std;
int f[N][],sum[N];
int A[],vis[N][];
int ok(int x) //测试当前状态下是否只剩下一种巧克力,是的话,二进制状态里必定只有一个1,因此只会返回1 否则则返回其他值.
{
if (x==) return ;
return ok(x/)+(x&);//这里导致我WA了好几次,原因是没注意&的优先级低,要括号起来
}
int dp(int S,int x)
{
if (vis[S][x]) return f[S][x];
vis[S][x]=;
//int& ans=f[S][x];
if (ok(S)==) return f[S][x]=; //如果满足,则说明以及到达某块具体巧克力的状态,完成任务 return 1回去
int y=sum[S]/x;
for (int s0=S&(S-);s0;s0=S&(s0-))//枚举S的子集
{
int s1=S-s0;
if (sum[s0]%x== && dp(s0,min(x,sum[s0]/x)) && dp(s1,min(x,sum[s1]/x)))
return f[S][x]=;//这里分两种情况,分别代表两个子集的产生 是切割y 或者 切割x产生的
if (sum[s0]%y== && dp(s0,min(y,sum[s0]/y))&& dp(s1,min(y,sum[s1]/y)))
return f[S][x]=;
}
return f[S][x]=; }
int main()
{
int kase=,n,x,y;
while (scanf("%d",&n)!=EOF)
{
if (n==) break;
scanf("%d%d",&x,&y);
for (int i=;i<n;i++)
scanf("%d",&A[i]);
memset(sum,,sizeof sum);
for (int i=;i<(<<n);i++)
{
for (int j=;j<n;j++)
{
if (i&(<<j))
{
sum[i]+=A[j];
}
}
}
memset(vis,,sizeof vis);
int all=(<<n)-;
int ans=;
if (sum[all]!=x*y || sum[all]%x!=)
{
ans=;
}
else
{
ans=dp(all,min(x,y));
}
printf("Case %d: %s\n",++kase,ans==? "Yes":"No");
}
return ;
}

UVALive 4794 Sharing Chocolate DP的更多相关文章

  1. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

  2. UVALive 4794 Sharing Chocolate

    Sharing Chocolate Chocolate in its many forms is enjoyed by millions of people around the world ever ...

  3. LA 4794 - Sharing Chocolate dp

    题意 有一块\(x*y\)的巧克力,问能否恰好分成n块,每块个数如下 输入格式 n x y a1 a2 a3 ... an 首先\(x \times y 必然要等于 \sum\limits_{i=1} ...

  4. UVALive 4794 Sharing Chocolate(状压,枚举子集)

    n的规模可以状压,f[x][y][S]表示x行,y列,S集合的巧克力能否被切割. 预处理出每个状态S对应的面积和sum(S),对于一个合法的状态一定满足x*y=sum(S),实际上只有两个变量是独立的 ...

  5. LA 4794 Sharing Chocolate

    大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...

  6. UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. UVa 1009 Sharing Chocolate (数位dp)

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  8. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

  9. UVALive - 6529 找规律+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...

随机推荐

  1. Acwing199 余数之和

    原题面:https://www.acwing.com/problem/content/201/ 题目大意:给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 ...

  2. bzoj 4008、4011、1499

    全是扒题解,,,太弱了... 不乱BB了. 4008 #include <bits/stdc++.h> #define LL long long #define lowbit(x) x&a ...

  3. 一百一十、SAP的OO-ALV之四,定义屏幕相关变量和逻辑流

    一.代码如下,定义相关变量 二.来带屏幕页面,双击STATUS_9000和USER_COMMAND_9000,自动生成相应代码 三.点击是 四.会自动生产关联的Includ文件 五.我们自己创建一个M ...

  4. Spark 资源调度包 stage 类解析

    spark 资源调度包 Stage(阶段) 类解析 Stage 概念 Spark 任务会根据 RDD 之间的依赖关系, 形成一个DAG有向无环图, DAG会被提交给DAGScheduler, DAGS ...

  5. CF1141D Colored Boots

    There are n left boots and n right boots. Each boot has a color which is denoted as a lowercase Lati ...

  6. webpack散记--Typescript

    Typescript 1.js的超集 官网:typescriptlang.org/tslang.cn 来自:微软 安装:官方的  npm i typescript ts-loader --save-d ...

  7. 【剑指Offer】面试题24. 反转链表

    题目 定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3 ...

  8. python脚本文件引用

    二.Python __init__.py 作用详解 https://www.cnblogs.com/Lands-ljk/p/5880483.html __init__.py 文件的作用是将文件夹变为一 ...

  9. 国内OLED产业与三星到底是差之千里还是近在咫尺?

    此前,市面上几乎大部分智能手机搭载的刘海屏,都是来自三星的AMOLED屏幕.虽然三星总是被诟病为中国手机厂商提供的是"次品",不过没办法,OLED屏幕的核心技术.产能等都掌握在三星 ...

  10. ORACLE时间相关问题

    一.遇到一个情况数据库存入的时间格式为20180831154546,需要将其转化为日期时分秒格式的. to_date('20180831154546', 'yyyy-mm-dd hh24:mi:ss' ...