题面

Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk each day (1 <= M(i) <= 100,000,000).

FJ wants to streamline the process of milking his cows every day, so he installs a brand new milking machine in his barn.

Unfortunately, the machine turns out to be far too sensitive: it only works properly if the cows on the left side of the

barn have the exact same total milk output as the cows on the right side of the barn!

Let us call a subset of cows "balanced" if it can be partitioned into two groups having equal milk output.

Since only a balanced subset of cows can make the milking machine work, FJ wonders how many subsets of his N cows are balanced.

Please help him compute this quantity.

有多少个非空子集,能划分成和相等的两份。

题解

我在考场上打的是暴力\(3^n\),我不会告诉你我CE了

我们可以\(3^{n/2}\)枚举两边的子集,然后\(meeting\;in\;the\;middle\)即可

代码

#include<cstdio>
#include<map>
#include<vector>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x)); inline int read()
{
int data = 0, w = 1;
char ch = getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data*w;
} const int maxn(21);
int n, a[maxn], ok[1 << maxn], cnt, ans; typedef std::vector<int>::iterator iter;
std::map<int, int> map;
std::vector<int> set[1 << maxn]; void dfs(int x, int s, int d)
{
if(x > (n >> 1) - 1)
{
if(map.find(d) == map.end()) map[d] = ++cnt;
int t = map[d]; set[t].push_back(s); return;
} dfs(x + 1, s, d);
dfs(x + 1, s | (1 << x), d + a[x]);
dfs(x + 1, s | (1 << x), d - a[x]);
} void Dfs(int x, int s, int d)
{
if(x > n - 1)
{
if(map.find(d) == map.end()) return;
int t = map[d];
for(RG iter it = set[t].begin(); it != set[t].end(); ++it) ok[(*it) | s] = 1;
return;
} Dfs(x + 1, s, d);
Dfs(x + 1, s | (1 << x), d + a[x]);
Dfs(x + 1, s | (1 << x), d - a[x]);
} int main()
{
n = read();
for(RG int i = 0; i < n; i++) a[i] = read();
dfs(0, 0, 0); Dfs((n >> 1), 0, 0);
for(RG int i = (1 << n) - 1; i; i--) ans += ok[i];
printf("%d\n", ans);
return 0;
}

SPOJ11469 SUBSET的更多相关文章

  1. SPOJ11469 Subset(折半枚举)

    题意 给定一个集合,有多少个非空子集,能划分成和相等的两份.\(n\leq 20\) 题解 看到这个题,首先能想到的是\(3^n\)的暴力枚举,枚举当前元素是放入左边还是放入右边或者根本不放,但是显然 ...

  2. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  3. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  4. 洛谷 P1466 集合 Subset Sums Label:DP

    题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...

  5. LeetCode "Largest Divisible Subset" !

    Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...

  6. 【USACO 2.2】Subset Sums (DP)

    N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...

  7. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. ArcGIS制图之Subset工具点抽稀

    制图工作中,大量密集点显示是最常遇到的问题.其特点是分布可能不均匀.数据点比较密集,容易造成空间上的重叠,影响制图美观.那么,如果美观而详细的显示制图呢? Subset Features(子集要素)工 ...

随机推荐

  1. 【RabbitMQ】4、三种Exchange模式——订阅、路由、通配符模式

    前两篇博客介绍了两种队列模式,这篇博客介绍订阅.路由和通配符模式,之所以放在一起介绍,是因为这三种模式都是用了Exchange交换机,消息没有直接发送到队列,而是发送到了交换机,经过队列绑定交换机到达 ...

  2. 关于Struts2通配符无效的说明

    在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法. 1.struts2.5 为了增加安全性,在 struts.xm ...

  3. DQL、DML、DDL、DCL概念与区别

    SQL(Structure Query Language)语言是数据库的核心语言. SQL的发展是从1974年开始的,其发展过程如下:1974年-----由Boyce和Chamberlin提出,当时称 ...

  4. 编程算法 - 数字在排序数组中出现的次数 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/36869869 数字在排序数组中出现的次数 代 ...

  5. java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive

    问题:使用Spring时,报错:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive问题原因: 少了as ...

  6. 鼠标不能用怎么办 USB OPTICAL MOUSE

    刚买的新鼠标,一般鼠标插上去自动安装驱动,然后就可以正常使用了. 如果遇到下面这种情况:"usb optical mouse 找不到驱动程序" 插上以后死活都没作用,然后开始下载一 ...

  7. Page Object 设计模式-PO

    1.传统测试用例实现的弊端: 易读性差 复用性差 可维护性差 扩展性差 2.PO 设计模式图: 3.Page Object 的核心要素: 抽象封装一个 BasePage 基类,基类应该拥有一个只想 w ...

  8. Javascript--将十进制数字转换成罗马数字显示

    下午在FCC(FreeCodeCamp)中文网上做到一道练习题:将给定的数字转换成罗马数字.折磨了一个多小时,终于能把基本功能给实现了.过程如下: 关于罗马数字 罗马数字的详细介绍可见百度,或者罗马数 ...

  9. 课时87. !important(掌握)

    1.什么是important 作用:用于提升某个直接选中标签的选择器中的某个属性的优先级,可以将被指定的属性的优先级提升为最高. 注意点: 1.important只能用于直接选中,不能用于间接选中 p ...

  10. mongodb的docker化安装

    查询mongo镜像 docker search mongo 拉取镜像(拉取STARS最多的那个就可以了) docker pull mongo tips:如果拉取不成功,多pull几次就可以了. 使用自 ...