hdu 2082 多重背包简化版

来源:互联网 发布:便笺元数据损坏的原因 编辑:程序博客网 时间:2024/06/03 23:41

题意:

26个单词,他们的价值为1~26,现在给出他们的个数,求凑成价值50以下的单词有多少种凑法,单词无序。


解析:

dp[ i ][ j ] = ∑ dp[ i - 1 ] [ j - k * value[i] ]


代码:

#pragma comment(linker, "/STACK:1677721600")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <cassert>#include <iostream>#include <algorithm>#define pb push_back#define mp make_pair#define LL long long#define lson lo,mi,rt<<1#define rson mi+1,hi,rt<<1|1#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a,b) memset(a,b,sizeof(a))#define FIN freopen("in.txt", "r", stdin)#define FOUT freopen("out.txt", "w", stdout)using namespace std;const double eps = 1e-8;const double ee = exp(1.0);const int inf = 0x3f3f3f3f;const int maxn = 30 + 10;const int maxv = 20 * 26 + 10;const double pi = acos(-1.0);const LL iinf = 0x3f3f3f3f3f3f3f3f;int readT(){    char c;    int ret = 0,flg = 0;    while(c = getchar(), (c < '0' || c > '9') && c != '-');    if(c == '-') flg = 1;    else ret = c ^ 48;    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);    return flg ? - ret : ret;}int value[maxn];int amount[maxn];int dp[maxn][maxv];int main(){#ifdef LOCAL    FIN;#endif // LOCAL    int ncase = readT();    while (ncase--)    {        for (int i = 1; i <= 26; i++)        {            value[i] = i;            amount[i] = readT();        }        mem0(dp);        for (int i = 0; i <= 26; i++)        {            dp[i][0] = 1;        }        for (int i = 1; i <= 26; i++)        {            for (int j = 1; j <= 50; j++)            {                for (int k = 0; k <= amount[i]; k++)                {                    if (j - k * value[i] >= 0)                        dp[i][j] += dp[i - 1][j - k * value[i]];                    else                        break;                }            }        }        int ans = 0;        for (int i = 1; i <= 50; i++)            ans += dp[26][i];        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击