HDU1398(DP或者母函数)

题意是求n用平方数的和的表示方法种数。

水题怎么搞都行。

DP:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 41111

long long dp[311];
int a[22];
int n;

int main () {
    for (int i = 1; i <= 17; i++)
        a[i] = i*i;
    while (cin >> n && n) {
        memset (dp, 0, sizeof dp);
        for (int i = 1; i <= 17; i++) {
            dp[a[i]]++;
            for (int j = 1; j <= n; j++) {
                if (j+a[i] <= n) dp[j+a[i]] += dp[j];
            }
        }
        cout << dp[n] << endl;
    }
    return 0;
}

母函数:

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

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

int main () {
    //freopen ("data.txt", "r", stdin);
    while (cin >> n && 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*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[n] << endl;
    }
    return 0;
}


你可能感兴趣的:(HDU1398(DP或者母函数))