SP11469 SUBSET - Balanced Cow Subsets
2019-11-15 13:53 一只弱鸡丶 阅读(138) 评论(0) 编辑 收藏 举报#include <bits/stdc++.h> using namespace std; #define ll long long #define re register #define pb push_back #define fi first #define se second const int N=2e6+10; void read(int &a) { a=0;int d=1;char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } int n,m; int a[30],cnt,f[N]; map <int,int> vis; vector <int> v[N]; void dfs1(int i,int sum,int now) { if(i>=m) { if(!vis.count(sum)) vis[sum]=++cnt; v[vis[sum]].pb(now); return; } dfs1(i+1,sum,now); dfs1(i+1,sum+a[i],now|(1<<i)); dfs1(i+1,sum-a[i],now|(1<<i)); } void dfs2(int i,int sum,int now) { if(i>=n) { if(vis.count(sum)) { int x=vis[sum]; for(auto j:v[x]) f[j|now]=1; } return; } dfs2(i+1,sum,now); dfs2(i+1,sum+a[i],now|(1<<i)); dfs2(i+1,sum-a[i],now|(1<<i)); } int main() { read(n);m=n>>1; for(re int i=0;i<n;i++) read(a[i]); dfs1(0,0,0),dfs2(m,0,0); int ans=0; for(re int i=1;i<(1<<n);i++) ans+=f[i]; printf("%d",ans); return 0; }