题意:有一个天平,两个臂上有钩子,给出所有钩子的位置,给出每个钩码的重量(各不相同),求必须使用所有钩码的前提下,有多少种平衡方法。
分析:dp,f[i][j + 5000]表示用前i个砝码到达力矩j的方法数
f[i + 1][j + hook[k] * weight[i]] += f[i][j];
最终结果存储在f[n][5000]中。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
usingnamespace std;
#define maxn 25
#define maxw 10005
int n, m;
int hook[maxn], weight[maxn];
int f[maxn][maxw];
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i =0; i < n; i++)
scanf("%d", &hook[i]);
for (int i =0; i < m; i++)
scanf("%d", &weight[i]);
f[0][5000] =1;
for (int i =0; i < m; i++)
for (int j =0; j < maxw; j++)
if (f[i][j])
for (int k =0; k < n; k++)
f[i +1][j + hook[k] * weight[i]] += f[i][j];
printf("%d\n", f[m][5000]);
return0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
usingnamespace std;
#define maxn 25
#define maxw 10005
int n, m;
int hook[maxn], weight[maxn];
int f[maxn][maxw];
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i =0; i < n; i++)
scanf("%d", &hook[i]);
for (int i =0; i < m; i++)
scanf("%d", &weight[i]);
f[0][5000] =1;
for (int i =0; i < m; i++)
for (int j =0; j < maxw; j++)
if (f[i][j])
for (int k =0; k < n; k++)
f[i +1][j + hook[k] * weight[i]] += f[i][j];
printf("%d\n", f[m][5000]);
return0;
}