SPOJ11469 SUBSET
题面
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的更多相关文章
- SPOJ11469 Subset(折半枚举)
题意 给定一个集合,有多少个非空子集,能划分成和相等的两份.\(n\leq 20\) 题解 看到这个题,首先能想到的是\(3^n\)的暴力枚举,枚举当前元素是放入左边还是放入右边或者根本不放,但是显然 ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 洛谷 P1466 集合 Subset Sums Label:DP
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- LeetCode "Largest Divisible Subset" !
Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...
- 【USACO 2.2】Subset Sums (DP)
N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到 ...
- Leetcode 416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- ArcGIS制图之Subset工具点抽稀
制图工作中,大量密集点显示是最常遇到的问题.其特点是分布可能不均匀.数据点比较密集,容易造成空间上的重叠,影响制图美观.那么,如果美观而详细的显示制图呢? Subset Features(子集要素)工 ...
随机推荐
- POJ2720 Last Digits
嘟嘟嘟 一道题又写了近两个点-- 这道题直接暴力快速幂肯定会爆(别想高精),所以还是要用一点数学知识的- 有一个东西叫欧拉降幂公式,就是: \(x ^ y \equiv x ^ {y \ \ ...
- Python 多线程 start()和run()方法的区别(三)
上一篇文章最后只是简单介绍了start()方法和run()方法,这篇文章再详细地看下start()和run()的区别. 在实例调用的函数中加入打印当前线程的名字,分别用start()方法和run()方 ...
- leetcode231 2的幂 leetcode342 4的幂 leetcode326 3的幂
1.2的幂 正确写法: class Solution { public: bool isPowerOfTwo(int n) { ) return false; )) == ; } }; 错误写法1: ...
- valgrind massif内存分析[转]
valgrind检查内存泄露 #valgrind ./程序 内存泄漏问题,我们有memcheck工具来检查.很爽.但是有时候memcheck工具查了没泄漏,程序一跑,内存还是狂飙.这又是什么问题. ...
- PAT——1022. D进制的A+B
输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D. 输出格式: 输出A+B的D ...
- C语言函数指针 和 OC-Block
C语言函数指针 和 OC-Block 一. C语言函数指针 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/2967037 ...
- BLE CC2541 串口BootLoader 即 SBL BootLoader 资料 收集
1.[CC254X_Bootloader]SBL(串口Bootloader)使用说明 2.CC2540协议栈高速串口通信解决(UART的DMA方式) 3.[BLE]CC2541之SBL 4.[BLE] ...
- pt-archiver数据归档
可以使用percona-toolkit包中的pt-archiver工具来进行历史数据归档 pt-archiver使用的场景: 1.清理线上过期数据. 2.清理过期数据,并把数据归档到本地归档表中,或者 ...
- bootstrap世界探索1——山川河流(文字排版)
世界到底是什么?其实世界很简单,正所谓一花一世界,一树一菩提,世界就在我们身边.造物神是伟大的,在我看来无论是HTML,css,js都可以看作是一个世界,但是他们是构成宏观世界不可或缺的,正如IU框架 ...
- 针对shiro框架authc拦截器认证成功后跳转到根目录,而非指定路径问题
一.针对shiro框架authc拦截器认证成功后跳转到根目录,而非指定路径问题 首先,我们先来了解一下authc登录拦截器工作原理 authc拦截器有2个作用: 1>登录认证 请求进来时 ...