HDU_2079_选课时间(题目已修改,注意读题)
http://acm.hdu.edu.cn/showproblem.php?pid=2079
Problem Description
又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别)
Input
输入数据的第一行是一个数据T,表示有T组数据。
每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8 )。
接着有k行,每行有两个整数a(1 <= a <= 8 ),b(1 <= b <= 10),表示学分为a的课有b门。
Output
对于每组输入数据,输出一个整数,表示学n个学分的组合数。
Sample Input
2
2 2
1 2
2 1
40 8
1 1
2 2
3 2
4 2
5 8
6 9
7 6
8 8
Sample Output
2
445
#include <iostream>using namespace std;int dp[650], temp[650], M, num[10], w[10];void gfun (int cost, int amount){ int j, x; for (j = 0; j <= M; j++) //枚举构造对象 for (x = 0; j - x * cost >= 0 && x <= amount; x++) //枚举物品数 temp[j] += dp[j-x*cost]; //可以用递推的思想去理解 for (j = 0; j <= M; j++) //滚动数组 { dp[j] = temp[j]; //cout << dp[j] << ' '; temp[j] = 0; } //cout << endl;}int main(){ int i, n, t; scanf ("%d", &t); while (t--) { memset (temp, 0, sizeof(temp)); memset (dp, 0, sizeof(dp)); dp[0] = 1; scanf ("%d%d", &M, &n); for (i = 0; i < n; i++) { scanf ("%d%d", w+i, num+i); gfun (w[i], num[i]); } printf ("%d\n", dp[M]); } return 0;}