HDU2069(二维母函数)

题意是5种面值为1,5,10,25,50的硬币组成n块钱有多少种方法。

有两个坑点:硬币总数不能超过100个,规定n=0时输出1。

加一维硬币总数就行。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 333
#define maxm 1111111

long long a[maxn][111], b[maxn][111];
int n;
int c[11];

int main () {
    c[1] = 1, c[2] = 5, c[3] = 10, c[4] = 25, c[5] = 50;
        memset (a, 0, sizeof a);
        for (int i = 0; i < maxn && i <= 100; i++)
            a[i][i] = 1;
        for (int i = 2; i <= 5; i++) {
            memset (b, 0, sizeof b);
            for (int j = 0; j < maxn && j <= c[i]*100; j += c[i]) {
                for (int k = 0; k+j < maxn; k++) {
                    for (int l = j/c[i]; l <= 100; l++)
                        b[j+k][l] += a[k][l-j/c[i]];
                }
            }
            for (int j = 0; j < maxn; j++)
                for (int k = 0; k <= 100; k++)
                    a[j][k] = b[j][k];
        }
    while (cin >> n) {
        if (n == 0) {
            cout << "1" << endl;
            continue;
        }
        int ans = 0;
        for (int i = 1; i <= 100; i++)
            ans += a[n][i];
        cout << ans << endl;
    }
    return 0;
}


你可能感兴趣的:(HDU2069(二维母函数))