HDU 2079 dp解法

时间:2024-01-02 19:57:59

选课时间(题目已修改,注意读题)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5115    Accepted Submission(s): 3944

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
题意  中文题,不说了
题解   这题我们设一个dp[ i ]数组( i 表示学分数,dp[i]就是组成i学分的组合数),枚举每个课程,求出包括前面课程所能组成的学分,对数组进行更新
0.0......说的自己都不明白---_---!!!。
AC代码
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int dp[];
struct course
{
int s;
int n;
}f[];
int main(int argc, char const *argv[])
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++)
{
cin>>f[i].s>>f[i].n;
}
dp[]=;
for(int i=;i<=m;i++) //枚举课程
{
for(int j=n;j>=f[i].s;j--) //枚举价值,要从大到小推!
{
for(int k=;k<=f[i].n;k++) ////枚举这个物品的个数,如果当前枚举到的价值能放入此物品的话,就加上之前的价值
{
if(j>=k*f[i].s)
dp[j]+=dp[j-k*f[i].s]; //更新数组
else
break;
}
}
}
cout<<dp[n]<<endl;
}
return ;
}

另一种 从小到大推,别人写的,摘自http://www.lai18.com/content/2463208.html

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int main()
{
int T, n, num, a[], b[], c1[], c2[];
//由于1<=num<=8,所以a,b开到10;由于1<=n<=40,所以c1,c2开到41
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &num);
for (int i = ;i <= num;i++)
scanf("%d%d", &a[i], &b[i]);
memset(c1, , sizeof(c1));
memset(c2, , sizeof(c2));
c1[] = ;//由于本题对每个a[i]有个数限制,所以不能使数组c1初始化1【那样相当于默认学分为1的课程无限个】
//但是可以在相乘多项式前多乘一个1,所以赋值c1[0] = 1,同时下面是从i=1开始的
for (int i = ;i <= num;i++)
{//i表示乘到了第几项
for (int j = ;j <= n;j++)
{
for (int k = ;k + j <= n&&k <= b[i]*a[i];k += a[i])//a[i]是第i个多项式的单个课程学分
c2[k + j] += c1[j];
}
memcpy(c1, c2, sizeof(c1));//c2赋给c1
memset(c2, , sizeof(c2));//c2清零
}
printf("%d\n", c1[n]);
}
return ;
}

还可以用母函数写 可是我不会母函数 有时间去研究研究~