字串數
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3505 Accepted Submission(s): 855
Problem Description 一個A和兩個B一共可以組成三種字符串:"ABB","BAB","BBA".
給定若干字母和它們相應的個數,計算一共可以組成多少個不同的字符串.
Input 每組測試數據分兩行,第一行為n(1<=n<=26),表示不同字母的個數,第二行為n個數A1,A2,...,An(1<=Ai<=12),表示每種字母的個數.測試數據以n=0為結束.
Output 對於每一組測試數據,輸出一個m,表示一共有多少種字符串.
Sample Input
2
1 2
3
2 2 2
0
Sample Output
3
90
可以輕易推出公式 :(n1+n2+n3+...nn)!/(n1!+n2!+...+nn!);
代碼:
#include
#include
#define SIZE 30
typedef long long ll ;
int d[SIZE] ;
int ans[1000] , f[15];
void multiply(int c)
{
ans[0] = ans[1] = 1 ;
for(int i = 2 ; i <= c ; ++i)
{
int r = 0 ;
for(int j = 1 ; j <= ans[0] ; ++j)
{
ans[j] *= i ;
ans[j] += r ;
r = ans[j]/10 ;
ans[j] %= 10 ;
}
if(r != 0)
{
while(r)
{
ans[ans[0]+1] += r%10 ;
ans[0] = ans[0]+1 ;
r /= 10 ;
}
}
}
}
void divide(int n)
{
for(int i = 0 ; i < n ; ++i)
{
if(d[i] == 1)
continue ;
ll r = 0 ;
for(int j = ans[0] ; j > 0 ; --j)
{
r = r*10 + ans[j] ;
ans[j] = (int)(r/f[d[i]]) ;
r %= f[d[i]] ;
}
int j = ans[0] ;
while(!ans[j--]) ;
ans[0] = j+1 ;
}
}
int main()
{
int n ;
f[0] = f[1] = 1 ;
for(int i = 2 ; i < 15 ; ++i)
f[i] = f[i-1]*i ;
while(scanf("%d",&n) && n)
{
int c = 0;
memset(ans,0,sizeof(ans)) ;
for(int i = 0 ; i < n ; ++i)
{
scanf("%d",&d[i]) ;
c += d[i] ;
}
multiply(c) ;
divide(n) ;
for(int i = ans[0] ; i > 0 ; --i)
printf("%d",ans[i]) ;
puts("") ;
}
return 0 ;
}
/*
26
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
答案:
4304048360075613709828073573963439771561246234874986506307801024608438578682946287839555537779695114
6973463603476517994060086036378690699890922398850719933177533763394328048206442072300423203375709346
9667364765509110256937684504670401569909337603281490150010206952984854343148131819790936625546951803
7655784189390500543894622277567223351757133619614420271316220121236818547225642751651682556313600000
0000000000000000000
*/
與君共勉