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

Sample Output

3


直接搜复杂度\(O(3^n)\),显然不行,考虑折半搜索,分成两部分,这样复杂度变为\(O(2*3^{n/2})\),然后对两部分进行查找即可,细节见代码

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=20,M=6e4;
struct S1{
int val,sta;
void insert(int v,int s){val=v,sta=s;}
}A[M+10],B[M+10];
int v[N+10],cntA,cntB,n;
bool vis[(1<<N)+10];
bool cmp1(const S1 &x,const S1 &y){return x.val<y.val;}
bool cmp2(const S1 &x,const S1 &y){return x.val>y.val;}
void dfs(int x,int limit,int sta,int sum){
if (x>limit){
limit==n>>1?A[++cntA].insert(sum,sta):B[++cntB].insert(sum,sta);
return;
}
dfs(x+1,limit,sta,sum);
dfs(x+1,limit,sta|(1<<(x-1)),sum+v[x]);
dfs(x+1,limit,sta|(1<<(x-1)),sum-v[x]);
}
int main(){
n=read();
for (int i=1;i<=n;i++) v[i]=read();
dfs(1,n>>1,0,0),dfs((n>>1)+1,n,0,0);
sort(A+1,A+1+cntA,cmp1);
sort(B+1,B+1+cntB,cmp2);
int i=1,j=1,Ans=0;
while (i<=cntA&&j<=cntB){
while (j<=cntB&&-B[j].val<A[i].val) j++;
int tmp=j;
while (A[i].val+B[j].val==0){
if (!vis[A[i].sta|B[j].sta]) vis[A[i].sta|B[j].sta]=1,Ans++;
j++;
}
j=tmp,i++;
}
printf("%d\n",Ans-1);
}

[Usaco2012 Open]Balanced Cow Subsets的更多相关文章

  1. 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 ...

  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. bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)

    2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 462  Solv ...

  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

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

  6. bzoj2679:[Usaco2012 Open]Balanced Cow Subsets

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

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

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

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

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

  9. SPOJ-SUBSET Balanced Cow Subsets

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

随机推荐

  1. TeamCity - Docker创建

    // 创建Server docker run -it --name teamcity-server-instance \-v /home/tc_datadir:/data/teamcity_serve ...

  2. &quot;What&#39;s New&quot; WebPart in SharePoint

    "What's New" WebPart in SharePoint 项目描写叙述         这是一个自己定义WebPart,能够显示一个列表,这个列表项目是在SharePo ...

  3. FTRL (Follow-the-regularized-Leader)算法

    Online gradient descent(OGD) produces excellent prediction accuracy with a minimum of computing reso ...

  4. AES算法加密java实现

    package cn.itcast.coderUtils; import java.security.Key; import javax.crypto.Cipher; import javax.cry ...

  5. Ubuntu使用adb连接android手机失败unknown的解决的方法

    Ubuntu使用adb连接android手机失败unknown的解决的方法   Ubuntu下通过USB数据线连接G11手机后,adb可能无法识别到设备.依照一下步骤能够解决此问题. 1.在termi ...

  6. 基于Servlet API 如何实现转向时不再地址栏中显示转向后的地址?

    A:redirect() B:sendRedirect()  C:forward()  D:transform()

  7. Struts2自定义过滤器的小例子-入门篇

    创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...

  8. StyleBook皮肤控件的使用

    StyleBook 介绍及VICEN对皮肤控件的一些看法可以说StyleBook的出现,简直是皮肤控件厂商的噩梦,因为用户可以通过StyleBook快速切换控件样式,而不需要在去购买第三方换肤控件,对 ...

  9. 加载之ready和onload

    页面加载完成有两种事件,一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件),二是onload,指示页面包含图片等文件在内的所有元素都加载完成. 真不知道这个标题该怎么取,暂时就先凑 ...

  10. 《Visual C++ 2010入门教程》系列三:关于C++的一些问题

    <Visual C++ 2010入门教程>系列三:关于C++的一些问题   这一回我自己都不知道应该写点什么好,或许今天的篇幅会比往常短很多.我说过,这不是C++的教程,因为我还没有那个能 ...