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 来,我们先来放松下,听听儿歌,一起“唱” ...
随机推荐
- ubuntu常用软件命令
解压zip软件 unzip xxx.zip -d解压到指定目录 清理磁盘空间 sudo apt-get autoremove sudo apt-get clean sudo dpkg --list ...
- Vue.js使用Leaflet地图
参考:https://blog.csdn.net/Joshua_HIT/article/details/72860171 vue2leaflet的demo:https://github.com/KoR ...
- Windows下安装MySql5.7(解压版本)
Windows下安装MySql5.7(解压版本) 1. 官方地址下载MySql Server 5.7 2. 解压文件到目录d:\Soft\mysql57下 3. 在上面目录下创建文件my.ini,内容 ...
- vue安装,router-link的一些属性,用法,tag active-class,to,replace,exex等等
第一步:$ npm install -g vue-cli 第二部:$ vue init webpack my-projectName 下面内容转载自:https://www.cnblogs.com/c ...
- CentOS 7 系统优化
系统调优4大子系统 1:找出系统中使用CPU最多的进程 2:找出系统中使用内存最多的进程 3:找出系统中对磁盘读写最多的进程 4:找出系统中使用网络最多的进程 系统调优概述 系统的运行状况: CPU ...
- C#异步编程基础入门总结
1.前言 *.NET Framework提供了执行异步操作的三种模式: 异步编程模型(APM)模式(也称为IAsyncResult的模式),其中异步操作要求Begin和End方法(例如,BeginWr ...
- php 根据日期获取星座
根据日期获取星座 /* * * 获取星座 * 星座是按阳历来计算的 * $month 阳历月份 * $day 阳历日期 * */ public static function get_xingzuo( ...
- JS设计模式(1)单例模式
什么是单例模式 定义:1.只有一个实例.2.可以全局访问 主要解决:一个全局使用的类频繁地创建与销毁. 何时使用:当您想控制实例数目,节省系统资源的时候. 如何解决:判断系统是否已经有这个单例,如果有 ...
- nmon 性能监控网页结果显示——EasyNmon
首先,看看最终展示的结果显示样式: 报告界面: 1.安装包下载地址:https://github.com/mzky/easyNmon 2.下载后有2个压缩文件: 其中,nmon16g_x86中含有不同 ...
- CSS布局学习(三) - position属性定义及解释(官网直译)
static ①元素的位置是在文档正常布局流中的位置. ②设置top right bottom left与z-index无效. ③在未指定position时,static是默认值 以下例子进行说明: ...