// This is to solve the exact knapsack pproblem based on // Algorithm 2. Array x is to hold binary sequences. // x[i]=1 means item i is chosen, x[i]=0, i not chosen. // The exact knapsack is to choose some a[i]'s so that // the sum of those can be equal to b. int b, i,n, s, t; int a[100],d[100],up[100], w[100], x[100]; out(int a[]) { int i; for(i=1; i<=n; i++)printf("%d ", a[i]); printf("\n"); } main(){ scanf("%d", &n); scanf("%d", &b); for(i=1;i<=n;i++) x[i]=0; for(i=1;i<=n;i++) a[i]=random()%100000; for(i=1;i<=n;i++) w[i]=random()%100; out(a); for(i=0;i<=n;i++) up[i]=i; s=0; t=clock(); do{ // out(x); i=0; while(x[i+1]==1){ x[i+1]=0; s=s-a[i+1]; i=i+1; } x[i+1]=1; s=s+a[i+1]; if(s==b){out(x); break;} } while (i!=n); printf("time %d\n", (clock()-t)/1000); }