HDU_2069 Coin Change(生成函数)

  没有看到Your program should be able to handle up to 100 coins. WA了n次。。。处理起来有点麻烦,看到大牛的思路是开一个二维数组c[i][j],表示用j个硬币组成大小为i的数。把j限定在100以下(有点dp的感觉,呵呵)。就可以了。详见代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 250;

int c1[N+1][N+1], c2[N+1][N+1];
int val[5] = {1, 5, 10, 25, 50};

void init() {
int i, j, k, l;
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));

for(i = 0; i <= N; i++) c1[i][i] = 1;

for(i = 1; i < 5; ++i) {
for(j = 0; j <= N; ++j)
for(k = 0; k*val[i] + j <= N; ++k)
for(l = 0; l + k <= 100; ++l) {
c2[k*val[i] + j][l + k] += c1[j][l];
}
for(j = 0; j <= N; ++j)
for(k = 0; k <= N; ++k) {
c1[j][k] = c2[j][k]; c2[j][k] = 0;
}
}
}

int main() {
//freopen("data.in", "r", stdin);

int n, i, sum;
init();
while(~scanf("%d", &n)) {
for(sum = 0, i = 0; i <= n && i <= 100; i++) {
sum += c1[n][i];
}
cout << sum << endl;
}
return 0;
}



你可能感兴趣的:(change)