俊介三

一天更新一点,一天积累一点

导航

HDUOJ-1398

Posted on 2013-03-09 13:20  俊介三在前进  阅读(87)  评论(0)    收藏  举报

Square Coins

http://acm.hdu.edu.cn/showproblem.php?pid=1398

使用面值为1,2^2,3^2,4^2,...,17^2的硬币若干个,能有多少个组合方式组合成给定的数目N

思路:母函数(generation function)的应用,构造(1+x+x^2+x^3...)(1+x^2+x^4+x^6+...)...(1+x^17+x^34+...)

得到x^N的系数即为所求解。

View Code
#include <stdio.h>

int result[305];
int temp[305];

int main(){
    int i,j,k,n;
    while(scanf("%d",&n)&&n){
        for(i=0;i<=n;i++){
            result[i]=1;
            temp[i]=0;
        }
        for(i=2;i<=17;i++){
            for(j=0;j<=n;j++){
                for(k=0;j+k<=n;k+=i*i){
                    temp[j+k] += result[j];
                }
            }
            for(j=0;j<=n;j++){
                result[j]=temp[j];
                temp[j]=0;
            }
        }
        printf("%d\n",result[n]);
    }
    return 0;
}