POJ - 1837 Balance

题意:有C个钩子,G个勾码,求让天平平衡的方法数,首先先确定最大的数组大小,假设全部加到一边,就是20*15*25=7500,还有因为有负数的情况所以,开到15000,用7500表示天平平衡的时候,dp[i][j]表示加i个砝码倾斜度是j的情况,递推,求最后的dp[G][7500]就行了

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 7505;

int c[21],w[21],dp[21][2*MAXN];
int n,m;

int main(){
    while (scanf("%d%d",&n,&m) != EOF){
        for (int i = 1; i <= n; i++)
            scanf("%d",&c[i]);
        for (int i = 1; i <= m; i++)
            scanf("%d",&w[i]);
        memset(dp,0,sizeof(dp));
        dp[0][7500] = 1;
        for (int i = 1; i <= m; i++)
            for (int j = 0; j < 2*MAXN; j++)
                if (dp[i-1][j])
                    for (int k = 1; k <= n; k++)
                        dp[i][j+w[i]*c[k]] += dp[i-1][j];
        printf("%d\n",dp[m][7500]);
    }
    return 0;
}



你可能感兴趣的:(POJ - 1837 Balance)