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(子集要素)工 ...
随机推荐
- python虚拟环境virtualenv高级篇
我曾经写过一篇virtualenv的博客:http://www.cnblogs.com/anpengapple/p/5907416.html 总体来讲还是适用的,不过稍微傻了一点.这一篇的内容有两个: ...
- swift中的"类型擦除"
代理模式.或者协议模式 因为swift泛型还不支持逆变和协变也就不会有真的类型擦除,而这里说的"类型擦除"是指:利用一个具体实现的通用泛型类(参看系统库的AnySequence), ...
- BZ2763: [JLOI2011]飞行路线
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
- webpack中Entry与Output的基础配置
entry顾名思义,就是打包的入口文件 module.exports = { // 这个文件要做打包,从哪一个文件开始打包 entry: './src/index.js', // 打包文件要放到哪里去 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- servlet的生命周期详解
一.servlet生命周期原理解析 1.Servlet生命周期分为三个阶段: (1)初始化阶段 调用init()方法 (2)响应客户请求阶段 调用service()方法 (3)终止阶段 调用dest ...
- 10-RabbitMQ-整合SpringBoot
RabbitMQ整个SpringBoot SpringBoot因其配置简单.快速开发,已经成为热门的开发之一 消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发送信息 而消 ...
- 把dataTable数据转换为Html
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...
- sqoop2启动job报错
set option --name verbose --value true #打开报错详情输出 Exception has occurred during processing command ...
- Selenium自动化测试值环境搭建
Selenium自动化测试之环境搭建 一.背景介绍 自动化测试近几年在测试领域很火,出去面试要是说不会自动化测试薪资都不好意思往高了要!很多公司做敏捷测试用到自动化,其他一些公司也是跟风,即使用不上自 ...