The main topic: There are N (1<=n<=16) species of monsters, each need to have to destroy it, one or several weapons to destroy, initially with the initial weapons, destroy a monster will acquire some weapons, ask to eliminate the total number of monsters in the order of how many.
Use D[s] to indicate how many sequences of monsters have been destroyed by the state of S (binary), using c[s] to represent weapons that have been wiped out after S. D[s] by enumerating which monster is finally eliminated, the precondition is to destroy all the weapons that have been removed except for the monsters that have been destroyed.
C[s] is calculated by taking the lowest bit, i.e. c[s]=c[s&-s]|c[s-(s&-s)].
#include <stdio.h> #include <stdlib.h>typedef long long Ll;char a[30];int c[77000]; LL D[77000];int Main (void) {int i,j,u,p,n,pi,qi; LL sump;scanf ("%d", &pi), for (qi=1;qi<=pi;qi++) {scanf ("%d", &n), C[0]=0;for (i=0;i<=n;i++) {scanf ("%s", a+1); U=0;for (j=n;j>=1;j--) {u=u*2+a[j]-' 0 ';} c[(1<<i)/2]= (u|c[0]);} p= (1<<n);d [0]=1;for (i=1;i<p;i++) {if ((i&-i)!=i) {c[i]= (c[i&-i]|c[i-(i&-i)]);} Sump=0;for (j=1;j<=n;j++) {u= (1<< (j-1)), if (((i&u)!=0) && ((c[i-u]&u)!=0)) {Sump=sump+d[i-u ];}} D[i]=sump;} printf ("Case%d:%lld\n", qi,d[p-1]);} return 0;}
UVA 11795-mega man ' s Mission (state compression DP)