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

题目描述

People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, …, and 289-credit coins, are available in Silverland.
There are four combinations of coins to pay ten credits:

ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.

Your mission is to count the number of ways to pay a given amount using coins of Silverland.

思路

和前面那题差不多,母函数$G(x)=(1+x+x^2+x^3+\cdots+x^N)(1+x^4+x^8)(1+x^9+x^{18})$…

AC代码

/*************************************************************************
    > File Name: 1398.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: 2015-06-30 Tue 22:16:36 CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;
const int _max = 300;

int main()
{
#ifdef Oj
    // freopen(".in", "r", stdin);
#endif
    int cs[18];
    for(int i=1; i<=17; ++i)
        cs[i] = i*i;
    int c1[_max+1], c2[_max+1];
    int V;
    while(cin >> V&& V) {
        for(int i=0; i<=V; ++i) {
            c1[i] = 1;
            c2[i] = 0;
        }
        for(int i=2; i<=sqrt(V); ++i) { // 这里应该小于等于sqrt(V)
            for(int j=0; j<=V; ++j)
                for(int k=0; k+j<=V; k+=cs[i]) {
                    c2[k+j] += c1[j];
                }
            for(int j=0; j<=V; ++j) {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
        cout << c1[V] << endl;
    }
    return 0;
}