母函数 HDU 2082 找单词
题意是,给出A~Z字母的个数,计算它们能组成单词的价值<=50的个数
也是母函数模板,判断时添加了一个最大值
#include<iostream> using namespace std; struct element { int numvalue; int maxnum; }; int main() { int hold[26]; element num[26]; int tmp[100]; int a[100]; int N; int n; int sum; freopen("C:\\Users\\Haojian\\Desktop\\test.txt", "r", stdin); cin >> N; for (int i = 0; i < N; i++) { n = 0; sum = 0; for (int i = 0; i < 100; i++) { tmp[i] = 0; a[i] = 0; } for (int i = 0; i < 26; i++) { cin >> hold[i]; if (hold[i]) { num[n].maxnum = hold[i]; num[n].numvalue = i+1; n++; }//初始化,记录每个字母的价值和最大个数 } for (int i = 0; i <= num[0].maxnum * num[0].numvalue; i += num[0].numvalue) tmp[i] = 1; //母函数 for (int i = 2; i <= n; i++) { for (int j = 0; j <= 50; j++) //添加一个最大个数的判断 for (int k = 0; k*(num[i-1].numvalue) + j <= 50 && k <= num[i-1].maxnum; k++) a[ k*(num[i-1].numvalue) + j] += tmp[j]; for (int j = 0; j <= 50; j++) { tmp[j] = a[j]; a[j] = 0; } } for (int j = 1; j < 51; j++) sum += tmp[j]; cout << sum << endl; } return 0; }