题面

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. 使用python 操作liunx的svn,方案二

    在对liunx操作svn的方式,做了改动,使用python的,subprocess进行操作 在第一种方案中,我使用了先拉到本地,然后再创建,在进行上传,实际在svn中可以直接创建文件,并进行文件复制, ...

  2. Codeforces Round #439 (Div. 2)【A、B、C、E】

    Codeforces Round #439 (Div. 2) codeforces 869 A. The Artful Expedient 看不透( #include<cstdio> in ...

  3. Mysql不带条件更新报错

    执行命令SET SQL_SAFE_UPDATES = 0;修改下数据库模式即可: 恢复安全模式:SET SQL_SAFE_UPDATES = 1;

  4. POJ3304 Segments

    嘟嘟嘟 题面就不说了,网上都有. 刚开始理解成了只要有不孤立的线段就算合法,结果就不会了--然而题中要求是所有线段至少有一个交点. 其实想一想就知道,问题转化为了是否存在一条直线和所有线段都有交点. ...

  5. PHP-----TP框架----命名空间

    TP框架----命名空间 命名空间,起什么作用??? [1]命名空间是一个虚拟的目录,这个文件有可能存在这个电脑里的任何一个地方,但是如果要把这个文件它的命名空间全部写成同一个那么这些文件就相当于在同 ...

  6. php反序列化

    之前听漏洞银行的一个女生讲php反序列化.她说了一句.php反序列话找public变量. 导致我以为必须php反序列化.可控的变量必须是public or protected.private私有的变量 ...

  7. xss实现获取内网ip

    前提得浏览器支持webRTC,测试的时候google浏览器测试成功,火狐浏览器不支持webRTC, 再在xss平台直接复制如下js代码: function form_ip(ip,port){ var ...

  8. js apply的用法

    问题: 1.apply和call的区别在哪里 2.什么情况下用apply,什么情况下用call 3.apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和ca ...

  9. Nexus修改admin密码及其添加用户

    Nexus之所以修改密码,是为了安全起见,个人学习的话,本地windows或者虚拟机即可,外网服务器建议将密码修改复杂点,而且强烈建议端口不要8081,最好将其改为其他的.同样也是为了安全起见. 添加 ...

  10. rocketmq双主发送消息 SLAVE_NOT_AVAILABLE 状态

    RocketMQ最佳实践之Producer 投递状态 发送消息时,将得到包含SendStatus的SendResult.首先,我们假设消息的isWaitStoreMsgOK = true(默认是tru ...