2679: [Usaco2012 Open]Balanced Cow Subsets

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 462  Solved: 197
[Submit][Status][Discuss]

Description

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.

给出N(1≤N≤20)个数M(i) (1 <= M(i) <= 100,000,000),在其中选若干个数,如果这几个数可以分成两个和相等的集合,那么方案数加1。问总方案数。

Input

 Line 1: The integer N. 
 Lines 2..1+N: Line i+1 contains M(i).

Output

* Line 1: The number of balanced subsets of cows.

Sample Input

4 1 2 3 4
INPUT DETAILS: There are 4 cows, with milk outputs 1, 2, 3, and 4.

Sample Output

3
OUTPUT DETAILS: There are three balanced subsets: the subset {1,2,3}, which can be partitioned into {1,2} and {3}, the subset {1,3,4}, which can be partitioned into {1,3} and {4}, and the subset {1,2,3,4} which can be partitioned into {1,4} and {2,3}.

HINT

 

Source

/*
判断能否划分为两个相等集合时用dp RE了
*/
#include<bits/stdc++.h> #define N 30
#define M 3111111
#define mod 2333333 using namespace std;
int n,m,ans,cnt,flag;
int a[N],vis[N],V[M];
int cur[N],sum[N]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(x=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} bool dfs2(int cur[],int k,int val,int n)
{
if(n== && cur[]!=cur[]) return false;
if(flag) return true;
if(val==sum[n]-val) {flag=;return true;}
if(k==n && !flag) return false;
for(int i=k+;i<=n;i++)
dfs2(cur,i,val+cur[i],n),dfs2(cur,i,val,n);
if(!flag)return false;
} bool judge()
{
int cnt_=,S=;
memset(cur,,sizeof cur);
memset(sum,,sizeof sum);
for(int i=;i<=n;i++) if(vis[i]) cur[++cnt_]=a[i],sum[cnt_]=sum[cnt_-]+cur[cnt_];
sort(cur+,cur+cnt_+);
for(int i=;i<=cnt_;i++) S+=S*+cur[i],S%=mod;
if(V[S]) return false;V[S]=;flag=;
if(sum[cnt_]%) return false;
if(dfs2(cur,,,cnt_)) return true;
return false; } void dfs(int lim,int k,int tot)
{
if(tot==lim)
{
if(judge()) ans++;
return;
}
if(k>n) return;
for(int i=k+;i<=n;i++)
{
if(vis[i]) continue;
vis[i]=;dfs(lim,k+,tot+);
vis[i]=;
}
} int main()
{
//freopen("ly.in","r",stdin);
n=read();
for(int i=;i<=n;i++) a[i]=read();
cnt=;
while(cnt<=n)
{
memset(vis,,sizeof vis);
dfs(cnt,,);
cnt++;
}
printf("%d\n",ans);
return ;
}

24暴搜

/*
折半搜索
枚举每个数如何选择,放入A就加,放入B就减
状压判断每个数的具体选择状态
最后双指针扫统计答案 若集合A的和 + 集合B的和为0那么就说明这两个集合构成的答案合法
*/
#include<bits/stdc++.h> #define N 22
#define ll long long using namespace std;
int n,v[N<<],maxdep,cnta,cntb;
bool vis[<<N];
ll ans;
struct node{
int state,x;
}a[<<N],b[<<N];
inline bool cmp1(node a,node b){return a.x<b.x;}
inline bool cmp2(node a,node b){return a.x>b.x;} inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(x=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void dfs(int dep,int sum,int now,int flag)
{
if(dep==maxdep+)
{
if(!flag)
a[++cnta].x=sum,a[cnta].state=now;
else
b[++cntb].x=sum,b[cntb].state=now;
return;
}
dfs(dep+,sum,now,flag);
dfs(dep+,sum+v[dep],now | (<<(dep-)),flag);
dfs(dep+,sum-v[dep],now | (<<(dep-)),flag);
}
int main()
{
n=read();
for(int i=; i<=n; i++)v[i]=read();
maxdep=n/;dfs(,,,);
maxdep=n; dfs(n/+,,,);
sort(a+,a++cnta,cmp1);
sort(b+,b++cntb,cmp2); int l=,r=;
while(l<=cnta&&r<=cntb)
{
while(-a[l].x<b[r].x&&r<=cntb)r++;
int pos=r;
while(r<=cntb&&-a[l].x==b[r].x)
{
if(!vis[a[l].state | b[r].state])
{
vis[a[l].state | b[r].state]=;
ans++;
}r++;
}
if(l<cnta&&a[l].x==a[l+].x)r=pos;
l++;
}
printf("%lld\n",ans-);//减去空集
return ;
}

bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)的更多相关文章

  1. BZOJ2679 : [Usaco2012 Open]Balanced Cow Subsets

    考虑折半搜索,每个数的系数只能是-1,0,1之中的一个,因此可以先通过$O(3^\frac{n}{2})$的搜索分别搜索出两边每个状态的和以及数字的选择情况. 然后将后一半的状态按照和排序,$O(2^ ...

  2. 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)

    [Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...

  3. BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针

    BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针 Description Farmer John's owns N ...

  4. 折半搜索+Hash表+状态压缩 | [Usaco2012 Open]Balanced Cow Subsets | BZOJ 2679 | Luogu SP11469

    题面:SP11469 SUBSET - Balanced Cow Subsets 题解: 对于任意一个数,它要么属于集合A,要么属于集合B,要么不选它.对应以上三种情况设置三个系数1.-1.0,于是将 ...

  5. bzoj2679:[Usaco2012 Open]Balanced Cow Subsets

    思路:折半搜索,每个数的状态只有三种:不选.选入集合A.选入集合B,然后就暴搜出其中一半,插入hash表,然后再暴搜另一半,在hash表里查找就好了. #include<iostream> ...

  6. [Usaco2012 Open]Balanced Cow Subsets

    Description Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk ...

  7. 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets

    [算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...

  8. SPOJ-SUBSET Balanced Cow Subsets

    嘟嘟嘟spoj 嘟嘟嘟vjudge 嘟嘟嘟luogu 这个数据范围都能想到是折半搜索. 但具体怎么搜呢? 还得扣着方程模型来想:我们把题中的两个相等的集合分别叫做左边和右边,令序列前一半中放到左边的数 ...

  9. BZOJ.2679.Balanced Cow Subsets(meet in the middle)

    BZOJ 洛谷 \(Description\) 给定\(n\)个数\(A_i\).求它有多少个子集,满足能被划分为两个和相等的集合. \(n\leq 20,1\leq A_i\leq10^8\). \ ...

随机推荐

  1. 用二分法计算a的n次幂<算法分析>

    实验目的:1.复习java编程:2.掌握二分法的基本原理:3.掌握使用java程序进行二分法计算a的n次幂.实验步骤:1.由用户输入a及n(均为整数):2.利用二分法完成计算,并将中间结果打印出来. ...

  2. [ 浙江大学 程序设计专题 ] 四个专题代码 报告 PPT共享

    [原创]转载请注明出处,请勿用于作弊 专题一: 链接: https://pan.baidu.com/s/11xCwvuPHDkTPeOB_yzJWnw 提取码: prup 专题二: 链接: https ...

  3. Linux下汇编语言学习笔记61 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  4. js数组转换成json串 (JSON.stringify)

    例如: var giftlist[1490011777] = []; giftlist[1490011777]['id'] = 1490011777; giftlist[1490011777]['na ...

  5. CODEFORCES problem 105A.Transmigration

    题目本身上手并不难,字符串处理+简单的排序.要注意的地方是浮点数的处理. 依据计算机中浮点数的表示原理,在实际编程的过程中即使用一个确定的整数(假设是1)给一个浮点变量赋值 在查看变量时会发现实际存储 ...

  6. T1003 电话连线 codevs

    http://codevs.cn/problem/1003/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市 ...

  7. SHARP 加粉

    http://v.ku6.com/show/7ufwm7pqfF8D_f13IdCaag...html

  8. spring-boot上传文件MultiPartFile获取不到文件问题解决

    1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现 multipartRequest.ge ...

  9. Eclipse的SVN插件 Subclipse

    原文:https://www.oschina.net/p/subclipse Subclipse 是一个为 Eclipse IDE 添加 Subversion 支持的项目.支持几乎所有版本的Eclip ...

  10. [转] ASPNET Core 中获取应用程序物理路径

    如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径,如下所示: // Classi ...