Description
夏令营有N个人,每个人的力气为M(i)。请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法?
Input
第一行一个整数N,表示人数。
接下来N行,每行一个整数M(i)
Output
输出一行一个整数,表示一共多少种选法。
Sample Input
4
1
2
3
4
Sample Output
3
样例解释:
第一种选出{1,2,3},分成{1,2}和{3}两组;
第二种选出{1,3,4},分成{1,3}和{4}两组;
第三种选出{1,2,3,4},分成{1,4}和{2,3}两组。
Data Constraint
40%的数据满足:1<=M(i)<=1000;
对于100%的数据满足:2<=N<=20,1<=M(i)<=100000000
剖解题目
。。。。
解法
直接暴力是3^20,显然过不了。
可以折半搜索。
每一个数只有选入第一集合,第二个集合或不选三种情况,分别用1,-1,0表示。
所以我们先3^10搜索前面的一半,记录所有状态。然后再搜索后面一半,看看是否有与前面状态一样的情况,记录答案。
当然,这有重复问题,我们用hash判重。
然而我人品太辣鸡,hash判重都被卡。。。。。。
可能是我连边问题。。。。
代码
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=(1<<20)+5,mo=1079021;
int a[25],ha[mo*2],fre[mo*2],next[mo*2],go[mo*2];
int n,num,ans;
bool bz[maxn];
int hash(int x)
{
int y=x%mo+mo;
while (ha[y]!=0&&ha[y]!=x) y=y%(mo*2)+1;
if (!ha[y]) ha[y]=x;
return y;
}
void add(int x,int y)
{
go[++num]=y;
next[num]=fre[x];
fre[x]=num;
}
void dfs1(int now,int sum,int sta)
{
if (now>n/2) {
int h=hash(sum);
add(h,sta);
return;
}
dfs1(now+1,sum,sta);
dfs1(now+1,sum+a[now],sta+(1<<now-1));
dfs1(now+1,sum-a[now],sta+(1<<now-1));
}
void dfs2(int now,int sum,int sta)
{
if (now>n){
int h=hash(sum);
int i=fre[h];
while (i){
if (!bz[go[i]+sta]) bz[go[i]+sta]=true,++ans;
i=next[i];
}
return;
}
dfs2(now+1,sum,sta);
dfs2(now+1,sum+a[now],sta+(1<<now-1));
dfs2(now+1,sum-a[now],sta+(1<<now-1));
}
int main()
{
freopen("subset.in","r",stdin);
freopen("subset.out","w",stdout);
scanf("%d",&n);
fo(i,1,n) scanf("%d",&a[i]);
dfs1(1,0,0);
dfs2(n/2+1,0,0);
printf("%d",ans-1);
}