虽然是一道水题,但鉴于网上很多题解都是用的母函数(本渣不会。。。),而且所用方法具有一般性(解HDU-1992也是用的此法),再加上很久没写博客(强行再加个理由),故记之。
思路:暴力!枚举每个字母的在单词中出现的个数,但为了提高效率,需采用记忆化搜索。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int num[30];
int dp[30][53]; // dp[pos][sum]表示用编号小于等于pos的字母组成价值小于sum的单词数,A编号为0,B编号为1…
int DFS(int pos, int sum)
{
if(pos < 0 && sum >= 0) return 1;
int& ans = dp[pos][sum];
if(ans) return ans;
for(int i = 0, top = min(sum/(pos+1), num[pos]); i <= top; i++)
{
ans += DFS(pos-1, sum-i*(pos+1));
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp, 0, sizeof(dp));
for(int i = 0; i < 26; i++) scanf("%d", &num[i]);
int ans = DFS(25, 50)-1; // 减1是因为不能一个字母也不选
printf("%d\n", ans);
}
return 0;
}