UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2
题目
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的更多相关文章
- UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索
题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...
- UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)
Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or t ...
- 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- UVA - 10118Free Candies(记忆化搜索)
题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...
- 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...
随机推荐
- 非CI执行Allure2 trends空白问题
问题描述 未使用CI工具集成Aluure运行测试用例并生成Allure报告,多次执行后,trends是空白的,未展示出期望的趋势图 问题原因非CI工具,是通过命令 allure serve 展示报告 ...
- 使用AMBER中遇到的一些问题
1.读取蛋白问题 读取无配体pdb文件(loadpdb complex.pdb)时,出现一堆 FATAL: Atom .R<ARG >.A<HD1 > does not hav ...
- day02 Python列表的增删查改及常用操作
列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li = [‘alex’,12 ...
- sitecore 数字化营销-path funnel
路径分析器是一个应用程序,允许您查看联系人在浏览网站时所采用的各种路径.您可以查看联系人在转换目标并与广告系列互动时所采用的路径,让您深入了解哪些路径为每次转化提供最佳参与价值,以及哪些路径效率较低且 ...
- Java web开发环境搭配
1.安装并配置JDK环境(1)安装过程省略(建议安装在自己指定的统一目录下,方便后期查找). (2)配置环境变量 JAVA_HOME: C:\Java\jdk\jdk1.7.0_45 (jdk安装目 ...
- MongoDB运维心得(一)
问题:集群内部通信压力较大.出现在某一个节点创建普通表并插入数据,在其他点读的问题.会造成每次读表都要进行一次完整的数据传输. 前提: Mongodb处于Sharding Cluster状态. 造成原 ...
- Linux下系统时间函数、DST等相关问题总结(转)
Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...
- iptables 最终 第四章
转发 ,NAT 功能 Chain FORWARD 开启网卡转发功能: /proc/sys/net/ipv4/ip_forward #使用sysctl 开启内核转发功能 sysctl - 核心转发: / ...
- vim自动补全头注释与说明
做个笔记吧. .vimrc autocmd BufNewFile *.c,*.cpp,*.sh,*.py,*.java exec ":call SetTitle()" " ...
- JS设计模式(5)发布订阅模式
什么是发布订阅模式(观察者模式)? 定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 主要解决:一个对象状态改变给其他对象通知的问题,而且 ...