题目:http://acm.hdu.edu.cn/showproblem.php?pid=2079
此题采用母函数的知识求解,套用母函数模板即可:
http://blog.csdn.net/ten_sory/article/details/59483762
C++代码如下:
#include<iostream>
using namespace std;
int main()
{
int T,i,j,m;
cin >> T;
while(T--)
{
int MAX,k;
cin >> MAX >> k;
int *v = new int[k+1];
int *e = new int[k+1];
for(i=1;i<=k;i++)
cin >> v[i] >> e[i];
int *a = new int[MAX+1];
int *b = new int[MAX+1];
for(i=0;i<=MAX;i++)
{
a[i] = 0;
b[i] = 0;
}
a[0] = 1;
for(i=1;i<=k;i++)
{
for(j=0;j<=e[i] && j*v[i]<=MAX;j++)
for(m=0;m+j*v[i]<=MAX;m++)
b[m+j*v[i]]+=a[m];
for(m=0;m<=MAX;m++)
{
a[m] = b[m];
b[m] = 0;
}
}
cout << a[MAX] << endl;
}
return 0;
}
上述代码提交,可以通过.