Violence: Violence enumerate a few of which, following a set of 01 backpack
F [i] [j] indicates to the i items, with the number scheme backpack j capacity, f [i] [j] = f [i-1] [j] + f [i-1] [jw [ i]] O (n ^ 3)
Optimization: do not consider the disappearance of 01 start and a backpack,
Definition of G [i] [j] i is represented disappears, the capacity of the number of program j, g [i] [j] = f [n] [j] - not legal
Is up against the g [i] [j] - = g [i] [jw [i]
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int maxn=2005; 6 #define R register 7 int w[maxn],n,m; 8 int f[maxn][maxn],g[maxn][maxn]; 9 int main() 10 { 11 scanf("%d%d",&n,&m); 12 for(R int i=1;i<=n;++i) 13 scanf("%d",&w[i]); 14 f[0][0]=1; 15 for(R int i=1;i<=n;++i) 16 for(R int j=0;j<=m;++j){ 17 f[i][j]=f[i-1][j]%10; 18 if(j-w[i]>=0)(f[i][j]+=f[i-1][j-w[i]])%=10; 19 } 20 for(int I = . 1 ; I <= n-; I ++ ) 21 is for ( int J = 0 ; J <= m; J ++ ) { 22 is G [I] [J] = F [n-] [J]% 10 ; 23 is IF ( JW [i]> = 0 ) (G [i] [J] - = G [i] [JW [i]])% = 10 ; 24 } // when the i-th item is not selected, knapsack capacity j, If the selected i, by g [i] [jw [i ]] ( not selected from the group represented by i, backpack jw [i]) is obviously transferred to subtract 25 for ( int I = . 1 ; I <= n-; I ++ ) { 26 is for ( int J = . 1 ; J <= m; J ++ ) 27 printf("%d",(g[i][j]+10)%10); 28 puts(""); 29 } 30 }