杭电hdu 2069 Coin Change 母函数

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

母函数题真是博大精深,当限制硬币数量时,就又有了新的考验。此题就是,如何去限制呢?还得深思。

#include <stdio.h>
#include <string.h>

#define MAX 251

int c1[MAX][101], c2[MAX][101];

int res[MAX];

int num[6] = {0, 1, 5, 10, 25, 50};

void init()
{
	int i, j, k, p;
	memset(c1, 0, sizeof(c1));
	memset(c2, 0, sizeof(c2));
	c1[0][0] = 1;
	for(i = 1; i <= 5; i ++){
		for(j = 0; j < MAX; j ++){//最大指数是250
			for(k = 0; k*num[i]+j < MAX; k ++){
				for(p = 0; p + k <= 100; p ++){//限制硬币总数不超过100?
					c2[k*num[i]+j][p + k] += c1[j][p];
				}
			}
		}
		for(j = 0; j < MAX; j ++){
			for(k = 0;  k < 101; k ++){
				c1[j][k] = c2[j][k];
				c2[j][k] = 0;
			}
		}
	}
	for(i = 1; i < MAX; i ++){
		for(j = 1; j < 101; j ++){
			res[i] += c1[i][j];
		}
	}
	res[0] = 1;
}

int main()
{
	int n;
	init();
	while(scanf("%d", &n)!=EOF){
		printf("%d\n", res[n]);
	}
	return 0;
}


你可能感兴趣的:(c)