您的位置:首页 > 其它

HDU1028(母函数)

2015-10-04 20:30 246 查看
题目求一个整数有多少种和的表示方法,顺序不同视为同一种。

相当于有无数个重量为1,2,3....n的砝码,要求组成重量为n的方案数,这就是母函数水题了。

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

long long a[122], b[122];
int n, num;

int main () {
    //freopen ("data.txt", "r", stdin);
    while (cin >> n) {
        memset (a, 0, sizeof a);
        for (int i = 0; i <= n; i++)
            a[i] = 1;
        for (int i = 2; i <= n; i++) {
            memset (b, 0, sizeof b);
            for (int j = 0; j <= n; j += i) {
                for (int k = 0; k <= n; k++) {
                    if (j+k <= n)
                        b[j+k] += a[k];
                }
            }
            for (int j = 0; j <= n; j++)
                a[j] = b[j];
        }
        cout << a
 << endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: