题目

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. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  2. Asp.net Core Mvc EF- Migrations使用

    Migragtion的命令,左边是手动命令,右边是代码方式 首先来看命令方式: 创建一个mvc项目,默认已经集成了EF包 创建的项目包含了Microsoft.AspNetCore.Identity.E ...

  3. jQuery-form实现文件分步上传

    分步上传:当你需要提交两个及以上的文件,在一个文件成功后再提交另一个文件,并且最后需要提交所有文件的地址组成的数据 HTML: <form id="uploadVideoForm&qu ...

  4. 使用dbeaver查mysql的表会导致锁表的问题

    查询完成之后接着需要使用rollback,不然其它session没法执行语句.

  5. Linux 命令整理 (有不正确的随时补充)

    du 概述: Linux下命令,统计目录(或文件)所占磁盘空间的大小. 语法: du[-abcDhHklmsSx][-L<符号连接>][-X<文件>][--block-size ...

  6. 前端学习历程--localstroge

    一. localstorage的特性 1.需要ie8+ 2.浏览器中都会把localStorage的值类型限定为string类型,这个在对我们日常比较常见的JSON对象类型需要一些转换 3.local ...

  7. websocket 二合一

    server from flask import Flask, request, render_template from geventwebsocket.handler import WebSock ...

  8. Oarcle的开始

    1.数据库大致分类两种 1.关系型数据库(SQL) Oracle.Mysql(80%).DB2.Microsoft SQL Server.ProsgreSQL.Access.SQLSite 2.非关系 ...

  9. Redis学习-常用命令

    keys * 返回满足的所有键 exists key 是否存在指定的key,存在返回1,不存在返回0 expire key time 设置指定key的过期时间,可以使用ttl key查看剩余时间 pe ...

  10. Html br 标签

    Html br 标签 <html> <body> <!-- br标签:跳到下一行--> <p>内<br />容</p> 注:br ...