假設有x1個字母A, x2個字母B,..... x26個字母Z,同時假設字母A的價值爲1,字母B的價值爲2,..... 字母Z的價值爲26。那麼,對於給定的字母,可以找到多少價值<=50的單詞呢?單詞的價值就是組成一個單詞的所有字母的價值之和,比如,單詞ACM的價值是1+3+14=18,單詞HDU的價值是8+4+21=33。(組成的單詞與排列順序無關,比如ACM與CMA認爲是同一個單詞)。
Input
輸入首先是一個整數N,代表測試實例的個數。
然後包括N行數據,每行包括26個<=20的整數x1,x2,.....x26.
Output
對於每個測試實例,請輸出能找到的總價值<=50的單詞數,每個實例的輸出佔一行。
Sample Input
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
Sample Output
7 379297
題意:中文題,不過多敘述題意。
思路:這道題的話,也是套用一下母函數的模板即可。
AC代碼:
#include <bits/stdc++.h>
typedef long long ll;
const int maxx=10010;
const int inf=0x3f3f3f3f;
using namespace std;
int a[maxx],b[maxx];
int sup[maxx],temp[maxx];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
for(int i=0; i<26; i++)
{
scanf("%d",&a[i]);
b[i]=i+1;
}
int last=0,last2;
memset(sup,0,sizeof(sup));
sup[0]=1;
for(int i=0; i<26; i++)
{
last2=min(last+a[i]*b[i],50);
memset(temp,0,sizeof(int)*(last2+1));
for(int j=0; j<=a[i] && j*b[i]<=last2; j++)
{
for(int k=0; k<=last && k+j*b[i]<=last2; k++)
{
temp[k+j*b[i]]+=sup[k];
}
}
memcpy(sup,temp,sizeof(int)*(last2+1));
last=last2;
}
int sum=0;
for(int i=1; i<=50; i++)
if(sup[i])
sum+=sup[i];
printf("%d\n",sum);
}
return 0;
}