POJ_1837 Balance (dp)
我沙茶了,看了半天没看懂样例!后来问别人才知道。然后卑鄙,下流,无耻的看了别人的思路。无语了。
思路:dp[i][j],表示前i件物品挂上以后偏倚量为j时的情况数。总情况数为15*20*25 = 7500。所以dp[i:1~20][j:-7500 ~ 7500],可以用dp[i][j:0~15000];
My Code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5
6 using namespace std;
7
8 const int N = 15*20*25;
9 const int M = 21;
10
11 int f[M][N*2];
12 int a[M], b[M];
13
14 int main() {
15 //freopen("data.in", "r", stdin);
16
17 int c, g, i, j, k;
18 while(~scanf("%d%d", &c, &g)) {
19 for(i = 0; i < c; i++) scanf("%d", &a[i]);
20 for(i = 0; i < g; i++) scanf("%d", &b[i]);
21 memset(f, 0, sizeof(f)); f[0][N] = 1; //hook nothing
22 for(i = 1; i <= g; i++) {
23 for(j = 0; j <= N*2; j++) {
24 if(!f[i-1][j]) continue;
25
26 for(k = 0; k < c; k++) {
27 f[i][j + a[k]*b[i-1]] += f[i-1][j];
28 }
29 }
30 }
31 printf("%d\n", f[g][N]);
32 }
33 return 0;
34 }