HDU_2152 Fruit(生成函数)

  这里可以看成给一个面值为m的钱币,要求将它换成n种不同的面值都为1的钱币,同时要求每种钱币供应的上下限。然后就是生成函数模板了。。。

My Code:

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

using namespace std;

const int N = 110;

int c1[N], c2[N];
int MIN[N], MAX[N];

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

int n, m, i, j, k;
while(~scanf("%d%d", &n, &m)) {
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));

for(i = 1; i <= n; ++i) {
scanf("%d%d", &MIN[i], &MAX[i]);
}
c1[0] = 1;
for(i = 1; i <= n; ++i) {
for(j = 0; j <= m; ++j) {
for(k = MIN[i]; k + j <= m && k <= MAX[i]; ++k) {
c2[k + j] += c1[j];
}
}
for(j = 0; j <= m; ++j) {
c1[j] = c2[j]; c2[j] = 0;
}
}
cout << c1[m] << endl;
}
return 0;
}



你可能感兴趣的:(HDU)