题意:给出N个挂钩,M个砝码,每个挂钩有一个权值,可正可负(相当于天平的左右边).
求用M个砝码使天平平衡的方案数(砝码必须全用上).
分析:f[i,j]表示用前i个砝码,左右差值为j的方案数.转移见代码.
ans=f[m,0].
code:
const range=8000; r=7500; var f:array[0..21,-range..range] of longint; n,m,k,i,j:longint; p,w:array[0..21] of longint; begin readln(n,m); for i:=1 to n do read(p[i]); readln; for i:=1 to m do read(w[i]); for i:=1 to n do inc(f[1,w[1]*p[i]]); for i:=2 to m do for j:=1 to n do for k:=-r to r do f[i,k+w[i]*p[j]]:=f[i,k+w[i]*p[j]]+f[i-1,k]; writeln(f[m,0]); end.