HDOJ1261 大数乘除法应用

字串数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4794    Accepted Submission(s): 1275


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
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1263  1265  1264  1262  1260 

组合公式为(A1+A2+...+An)!/A1!/A2!/.../An!
显然是要用到大数乘除法了,我用的是100000进制,节约一下内存空间。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int mod = 1e5;
const int maxn = 200;
int n,ans,i,j,k,t,len;
int a[30],num[maxn];

int main(){
    while (scanf("%d",&n) && n) {
        ans = 0;
        for (i=0; i<n; i++) {
            cin >> a[i];
            ans += a[i];
        }
        memset(num,0,sizeof(num));
        num[0] = 1;
        len = 1;

        for (i=2; i<=ans; i++) {
            int x = 0;
            for (k=0; k<len; k++) {
                num[k] *= i;
                num[k] += x;
                x = 0;
                if (num[k]>mod) {
                    x = num[k]/mod;
                    num[k] %= mod;
                }
            }
            if (x!=0) num[len++] = x;
        }

        for (i=0; i<n; i++) {
            for (j=2; j<=a[i]; j++) {
                 int x = 0, y = 0;
                 for (k =len -1; k>=0; k--) {
                     y = x;
                     x = (num[k]+ y*mod)%j;
                     num[k] = (num[k]+ y*mod)/j;
                 }
                 while (num[len-1]==0) len--;
            }
        }
        while (num[len]==0) len--;
        printf("%d",num[len]);
        for (i=len-1; i>=0; i--) printf("%05d",num[i]);
        printf("\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值