OneN×MMatrix,Count (I, x)The last digit.
Sample Input3 2
1 1 2
Sample Output11
11
21
HINT
If item 3 is lost, there is only one way to fill the backpack with a capacity of 2, that is, choose item 1 and item 2.
Source
I had a good idea about DP and didn't come up with a solution... QAQ
F [I] [j] indicates the number of solutions for the first I items filled with a backpack with a volume of j.
Obviously, the f array can be calculated using the O (n ^ 2) DP.
G [I] [j] indicates that the I-th item is not selected, and the number of solutions for a backpack filled with a volume of j is displayed.
If j
If j is ≥n, g [I] [j] = f [n] [j]-g [I] [j-w [I].
We can find that both f and g Arrays can be implemented in one dimension.
At the beginning, WA performed many times, because no modulo was obtained for each step.
#include
#include
#include
#include
#include
#include#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define maxn 2005using namespace std;int n,m;int f[maxn],g[maxn],w[maxn];inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int main(){n=read();m=read();F(i,1,n) w[i]=read();memset(f,0,sizeof(f));f[0]=1;F(i,1,n) D(j,m,w[i]) f[j]+=f[j-w[i]];F(i,1,n){F(j,0,w[i]-1) g[j]=f[j];F(j,w[i],m) g[j]=f[j]-g[j-w[i]];F(j,1,m) printf("%d",g[j]%10);printf("\n");}}