Posted on 2010-08-07 21:00
MiYu 閱讀(1507)
評論(0) 編輯 收藏 引用 所屬分類:
ACM ( 母函數(shù) ) 、
ACM ( 水題 )
MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2082
題目描述:
Problem Description
假設(shè)有x1個字母A, x2個字母B,
.. x26個字母Z,同時假設(shè)字母A的價值為1,字母B的價值為2,
.. 字母Z的價值為26。那么,對于給定的字母,可以找到多少價值<=50的單詞呢?單詞的價值就是組成一個單詞的所有字母的價值之和,比如,單詞ACM的價值是1+3+14=18,單詞HDU的價值是8+4+21=33。(組成的單詞與排列順序無關(guān),比如ACM與CMA認(rèn)為是同一個單詞)。
Input
輸入首先是一個整數(shù)N,代表測試實(shí)例的個數(shù)。
然后包括N行數(shù)據(jù),每行包括26個<=20的整數(shù)x1,x2,
..x26.
Output
對于每個測試實(shí)例,請輸出能找到的總價值<=50的單詞數(shù),每個實(shí)例的輸出占一行。
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
題目分析:
有是一個很標(biāo)準(zhǔn)的母函數(shù)的題目 ( 更多母函數(shù)的了解 請看
<<母函數(shù)詳解>> ). 不過此題可以進(jìn)行適當(dāng)?shù)膬?yōu)化, 因?yàn)閿?shù)據(jù)是 26個數(shù)據(jù)順序輸入的,不是稀疏多項(xiàng)式,所以直接用一個變量輸入就行了.
代碼如下:
MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
#include <iostream>
using namespace std;
int num1[51];
int num2[51];
int main ()
{
int T;
while ( cin >> T )
{
while ( T -- )
{
memset ( num1, 0 , sizeof ( num1 ) );
memset ( num2, 0 , sizeof ( num2 ) );
num1[0] = 1;
for ( int i = 1; i <= 26; ++ i )
{
int cnt ;
cin >> cnt;
if ( cnt == 0 )
{
continue;
}
for ( int j = 0; j <= 50 ; ++ j )
{
for ( int k = 0; k <= cnt && k * i + j <= 50; k ++ )
{
num2[ k * i + j ] += num1[j];
}
}
for ( int j = 0; j <= 50; ++ j )
{
num1[j] = num2[j];
num2[j] = 0;
}
}
int total = 0;
for ( int i = 1; i <= 50; ++ i )
{
total += num1[i];
}
cout << total << endl;
}
}
return 0;
}