procedure2012
It's not worth it to know you're not worth it!

[题目来源]:Romania OI 2002

[关键字]:动态规划

[题目大意]:在一个天平中,有N个挂钩,挂钩距离在[-15..15]之间,-代表在左臂,+代表在右臂,挂钩距离依次从小到大给出。有M个砝码,砝码重量在[-25..25]之间,砝码重量依次从小到大给出。求,用上所有砝码将天平平衡的方法有多少种。

//=====================================================================================================

[分析]:将所有砝码与所有挂钩相乘,所得就是砝码挂在挂钩上时对平衡的贡献,以边为负另一边为正。天平平衡就是左右和为0,即用背包求出用所有砝码能使背包容量为0的方案数。此时背包容量最小值可能为负数,最大值也不是0,但求值方法不变。

[代码]:

View Code
 1 var
2 n, m: longint;
3 v, w: array[0..30] of longint;
4 f: array[0..30,-8500..8500] of int64;
5
6 procedure init;
7 var
8 i: longint;
9 begin
10 readln(n,m);
11 for i := 1 to n do
12 read(v[i]);
13 for i := 1 to m do
14 read(w[i]);
15 end;
16
17 procedure work;
18 var
19 i, j, k: longint;
20 begin
21 fillchar(f,sizeof(f),0);
22 f[0,0] := 1;
23 for i := 1 to m do
24 for j := 1 to n do
25 for k := -7500 to 7500 do
26 f[i,k] := f[i-1,k-w[i]*v[j]]+f[i,k];
27 writeln(f[m,0]);
28 end;
29
30 begin
31 init;
32 work;
33 end.



posted on 2011-11-02 08:16  procedure2012  阅读(191)  评论(0编辑  收藏  举报