标签:
2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 2 6 2 10 2 2 5 6 1 0 2 7 0 2 2 7 5 10 6 10 2 10 6 1 9
7 379297
题目大意:单词A-Z具有1-26的价值,现有字母A-Z的个数num[i],求问在不超过价值为五十的情况下,有多少种字母的组合数;
解题思路:母函数;用指数代表价值,价值又为数组的下标;用系数代表组成该价值的方案数,方案数为数组中存的值;
代码如下:
#include"iostream" #include"cstdio" #include"cstring" using namespace std; const int maxn = 50; int num[maxn + 1]; int c1[maxn + 1], c2[maxn + 1]; int main(){ int T; scanf("%d",&T); while(T --){ for(int i = 1;i <= 26;i ++) scanf("%d", &num[i]); memset(c1, 0, sizeof c1); memset(c2, 0, sizeof c2); for(int i = 0;i <= num[1];i ++) c1[i] = 1; for(int i = 2;i <= 26;i ++){ //共有26个多项式 if(num[i] == 0) continue; for(int j = 0;j <= maxn;j ++){ //共有maxn+1项 for(int k = 0;k <= num[i] && j + k*i <= maxn;k ++) c2[j + k*i] += c1[j]; } for(int j = 0;j <= maxn;j ++){ c1[j] = c2[j]; c2[j] = 0; } } int sum = 0; for(int j = 1;j <= maxn;j ++) sum += c1[j]; printf("%d\n",sum); } return 0; }
标签:
原文地址:http://blog.csdn.net/keeping111/article/details/51351571