码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1261

时间:2014-05-06 23:05:30      阅读:323      评论:0      收藏:0      [点我收藏+]

标签: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;
}


hdu1261,布布扣,bubuko.com

hdu1261

标签:hdu1261

原文地址:http://blog.csdn.net/fei____fei/article/details/25147631

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!