定义平衡度为天平的右边值减去左边值
则当平衡度为p时挂上一个重量为w的砝码,在c位置,平衡度会变为p+w*c
则平衡度为p+w*c的状态数应该加上平衡度为c时的状态数
#include <iostream>
#include <algorithm>
#include <cstring>
#include <functional>
using namespace std;
const int MAXN = 15005;
const int INF = 0x3f3f3f3f;
const int NIL = -1;
int C, G;
int hooks[MAXN];
int weights[MAXN];
int dp[25][MAXN]; //dp[i][j] 挂前i个砝码时平衡度为j的状态数
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> C >> G)
{
for (int i = 0; i < C; ++i)
cin >> hooks[i];
for (int i = 0; i < G; ++i)
cin >> weights[i];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < C; ++i)
dp[0][weights[0] * hooks[i] + 7500] = 1;
for (int i = 1; i < G; ++i)
{
for (int j = 0; j <= 15000; ++j)
{
for (int k = 0; k < C; ++k)
{
int t = hooks[k] * weights[i];
if (j + t >= 0 && j + t <= 15000)
dp[i][j + hooks[k] * weights[i]] += dp[i - 1][j];
}
}
}
cout << dp[G - 1][7500] << endl;
}
return 0;
}