The range of n is very small and can be associated with the enumeration subset and the pressure. But if you enumerate two subsets directly, it is obviously not enough. Then we can think of the binary enumeration! Meet inthe middle!
The n number is divided into two parts, A and a, and the answer subset is derived from the following types:
1. A subset of the set.
2. A subset of the collection of B.
3. A subset of A is part of a, and part of it is a subset of B.
For 1, 22 cases can be preprocessed directly:
Enumerates a subset of X, then enumerates subsets of X y, divides x into Y, and x^y two sets to determine whether their sums are equal.
For the 3rd case:
Suppose that A, B is in a set, C,d is in a set, and there is a+c=b+d. We can bashi the sub-item =>a-b=d-c, that is, each element in the A collection (the collection element is N1) has three possibilities: not fetch, to a set, Give a set of B. Then there is a total of 3n1 for a set. In the same way, the B-set has a 3N2 scheme, and the values of the schemes produced by each set are recorded with the selected elements and sorted by their values . For merging two ordered arrays:
For two sets with equal values, the intersection of them must satisfy the condition.
Optimization:
In the process of merging, you will find that there are many cases of duplication.
has a A-B state in a set and a b-a state. They correspond to d-c,c-d, but the intersection they produce is the same, {a,b,c,d}. So the b-a and c-d states are invalid, We can filter out a negative value when we are working on the state.
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <map
> #include <cstring> using namespace std;
const int M=20;
int a[m],mark[1<<m],n,f[1<<m],totc=0,totb=0; struct node{int val,p;}
B[60005],C[60005];
BOOL CMP (node A,node b) {return a.val<b.val;} int solve (int st,int t) {//determines the number of subsets within a set that satisfy the requirement int i,j,k,ans=0;
For (i=1;i< (1<<t); i++) {f[i]=0;
for (j=0;j<t;j++) if (i& (1<<J)) f[i]+=a[st+j]; For (j=i;j;j= (j-1) &i) {int a=j,b=i^j;//two subset if (a==0| |
b==0) continue;
if (F[a]==f[b]) {ans++;break;//}}} return ans;
} void get (int all,int l,int r,node t[],int &num) {int i,j,k; For (i=all;i;i= (i-1) &all) {for (j=i;j;j= (j-1) &i) {int a=j,b=i^j,now=0;//b collection to give the other for (k=l;k<r;k++) {if (a& (1<<k)) Now+=a[k];
else if (b& (1<<k)) now-=a[k];
} if (now>=0) t[++num]= (node) {now,i};
}} sort (t+1,t+1+num,cmp);
} int main () {int i,j,k,ans=0,stb=1,stc=1;
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%d", &a[i]);
int S1=N/2,S2=N-N/2;
Ans+=solve (0,S1) +solve (S1,S2);
Get ((1<<S1) -1,0,S1,B,TOTB);
Get (((1<<n)-1) ^ ((1<<s1)-1), S1,N,C,TOTC); while (STB<=TOTB&&STC<=TOTC) {//Merges two sequences while (Stc<=totc&&stb<=totb&&c[stc].val
!=b[stb].val) {while (c[stc].val<b[stb].val) stc++;
while (B[stb].val<c[stc].val) stb++; } if (stc>totc| |
STB>TOTB) break;
int c1=stc,b1=stb,x=c[stc].val;
while (c[stc].val==x) stc++;
while (b[stb].val==x) stb++; for (i=c1;i<stc;i++) for (j=b1;j<stb;j++) mark[c[i].p|
B[j].p]=1;
} for (i=0;i< (1<<n); i++) ans+=mark[i]; printf ("%d\n", ans);
return 0;
}