HDU 1284 钱币兑换问题(母函数,完全背包)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1284

思路:

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[40000];
int main()
{
    memset(dp,0,sizeof(dp));
    dp[0] = 1;
    for(int i=1; i<=3; i++)
    {
        for(int j=i; j<=32768; j++)
            dp[j] += dp[j-i];
    }
    int n;
    while(scanf("%d",&n) != EOF)
    {
        printf("%d\n",dp[n]);
    }
}

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
int c1[40000],c2[40000];
const int maxn = 32768;
int main()
{
    for(int i=0; i<=maxn; i++)//注意这里从1开始
    {
        c1[i] = 1;
        c2[i] = 0;
    }
    for(int i=2; i<=3; i++)
    {
        for(int j=0; j<=maxn; j++)
        {
            for(int k=0; k+j<=maxn; k+=i)
            {
                c2[k+j] += c1[j];
            }
        }
        for(int j=0; j<=maxn; j++)
        {
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
    int n;
    while(scanf("%d",&n) != EOF)
    {
        printf("%d\n",c1[n]);
    }
    return 0;
}


你可能感兴趣的:(HDU 1284 钱币兑换问题(母函数,完全背包))