题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2795

题意

x * y的巧克力,问能不能恰好切成n份(只能整数切),每块大小恰好ai

思路

明显,记忆化搜索。

这里参照刘书使用了sum来通过长计算宽

感想:

这种需要枚举子状态的题总是不敢枚举

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef tuple<int, int, int> MyTask;
typedef pair<int, double> MyPair;
const int MAXN = ;
const int MAXSTA = << ;
int ok[MAXN][MAXSTA];
long long sum[MAXSTA];
int n;
int a[MAXN];
int maxsta; int check(int r, int sta) {
if (ok[r][sta] != -)return ok[r][sta];
if (sum[sta] % r) return ok[r][sta] = ;
if ((sta & -sta) == sta)return ok[r][sta] = ;
int c = sum[sta] / r;
assert(r >= c);
for (int substa = (sta - ) & sta; substa != ; substa = (substa - ) & sta) {
if (sum[substa] * < sum[sta])continue;
if (sum[substa] % r == ) {
int othersubsta = sta ^ substa;
int subr = r;
int subc = sum[substa] / r;
int othersubr = r;
int othersubc = c - subc;
if (check(max(subr, subc), substa) && check(max(othersubr, othersubc), othersubsta)) {
return ok[r][sta] = ;
}
}
else if(sum[substa] % c == ){
int othersubsta = sta ^ substa;
int subr = sum[substa] / c;
int subc = c;
int othersubr = r - subr;
int othersubc = c;
if (check(max(subr, subc), substa) && check(max(othersubr, othersubc), othersubsta)) {
return ok[r][sta] = ;
}
}
}
return ok[r][sta] = ;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
int T;
//cin >> T;
for (int ti = ;cin>>n && n; ti++) {
memset(ok, -, sizeof ok);
int x, y;
cin >> x >> y;
for (int i = ; i < n; i++) {
cin >> a[i];
}
maxsta = << n;
for (int sta = ; sta < maxsta; sta++) {
sum[sta] = ;
for (int i = ; i < n; i++) {
if (sta & ( << i))sum[sta] += a[i];
}
}
if (sum[maxsta - ] == x * y && check(max(x, y), maxsta - )) {
printf("Case %d: Yes\n", ti);
}
else {
printf("Case %d: No\n", ti);
}
} return ;
}

UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2的更多相关文章

  1. UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索

    题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...

  2. UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)

     Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or t ...

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

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

  4. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  5. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  6. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  7. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  8. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  9. 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)

    心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...

随机推荐

  1. Fluxion无线攻击

    使用步骤 github地址 https://github.com/deltaxflux/fluxion 进入到fluxion目录下 ./fluxion 启动fluxion  启动之后会先检测没有安装的 ...

  2. 微信小程序之mpvue+iview踩坑之旅

    因为之前参照微信的原生的文档写过一些小程序的demo,写的过程比较繁琐,后来出了美团的mpvue,可以直接使用vue开发,其他的不作对比,这篇文章记录一下踩坑之旅. 参照mpvue http://mp ...

  3. 隐藏非选中的checkBox

    //隐藏非选中的checkBox function onlyCheckBox(){ $("#dtlTable tr:gt(0)").each(function(i) { var c ...

  4. 分配swap分区

    1.free命令 用来查看swap分区的使用情况[root@localhost ~]#free#查看内存与swap分区使用状况◆cached(缓存):是指把读取出来的数据保存在内存当中,当再次 读取时 ...

  5. 谷歌浏览器怎样把网页全部内容保存为.mhtml文件?

    Chrome保存.mhtml网页文件的方法: 在 Chrome 地址栏中键入chrome://flags,回车, 在页面搜索栏输入mhtml 把“Save Page as MHTML”项修改为 Ena ...

  6. Html fieldset、legend 标签

    Html fieldset.legend 标签 <html> <body> <!-- fieldset 添加圈起标题框标签 --> <fieldset> ...

  7. Linux 系统开启最大线程数 调优

    系统最大线程数说明 系统可开启的最大线程数,可根据系统本身负载配置进行调优. 查看系统最大线程数 1.查看系统开启的最大线程数. ulimit -u [root@izbp1brwu1w35r1dmj8 ...

  8. Link-Cut-Tree详解

    图片参考YangZhe的论文,FlashHu大佬的博客 Link-Cut-Tree实际靠的是实链剖分,重链剖分和长链剖分珂以参考树链剖分详解 Link-Cut-Tree将某一个儿子的连边划分为实边,而 ...

  9. echarts2.0tooltip边框限制导致tooltip显示不全解决办法

    1.显示常数位置x和y; 2.根据鼠标移动显示:tooltip : { trigger: 'axis', position:function(p){ //其中p为当前鼠标的位置 return [p[0 ...

  10. 关于乱码(MessyCode)问题

    乱码本质:读取二进制时采用的编码和最初将字符转成二进制时的编码不一致 编码时(得二进制数组时)不抛出异常,数据就不会被破坏 Java关于乱码(MessyCode)问题 Java使用的是Unicode编 ...