首页 > 代码库 > hdu1261

hdu1261

题意:一个A和两个B一共可以组成三种字符串:"ABB","BAB","BBA".
给定若干字母和它们相应的个数,计算一共可以组成多少个不同的字符串.
 
思路:
(a1+a2+ ... +an)! / a1! / a2! / ... / an!                  大数

 

 

#include <stdio.h>#include <string.h>const int MAX =505;struct BigNum{    int num[MAX];    int len;} a[51][51];BigNum Div(BigNum &a, int &b){    BigNum c;    int f;    int  i, len = a.len;    memset(c.num, 0, sizeof(c.num));    f = 0;    for(i = a.len-1; i >= 0; i--)    {        f = f*10+a.num[i];        c.num[i] = f/b;        f %= b;    }    while(len > 1 && c.num[len-1] == 0) len--;    c.len = len;    return c;}BigNum Mul1(BigNum &a, int  &b){    BigNum c;    int  i, len;    len = a.len;    memset(c.num, 0, sizeof(c.num));    //乘以0,直接返回0    if(b == 0)    {        c.len = 1;        return  c;    }    for(i = 0; i < len; i++)    {        c.num[i] += (a.num[i]*b);        if(c.num[i] >= 10)        {            c.num[i+1] = c.num[i]/10;            c.num[i] %= 10;        }    }    while(c.num[len] > 0)    {        c.num[len+1] = c.num[len]/10;        c.num[len++] %= 10;    }    c.len = len;    return  c;}int main(){    BigNum sum;    int i,j,m,p[26],s;    while(scanf("%d",&m),m)    {        s=0;        memset(p,0,sizeof(p));        for(i=0; i<m; i++)        {            scanf("%d",&p[i]);            s+=p[i];        }        for(i=1; i<MAX; i++)            sum.num[i]=0;        sum.num[0]=1;        sum.len=1;        for(i=2; i<=s; i++)            sum=Mul1(sum,i);        for(i=0; i<m; i++)            for(j=2; j<=p[i]; j++)                sum=Div(sum,j);        for(i=sum.len-1; i>=0; i--)            printf("%d",sum.num[i]);        printf("\n");    }    return 0;}