Divide and Count【排列组合】

8 篇文章 0 订阅
1 篇文章 0 订阅

题目描述

Jack has several beautiful diamonds, each of them is unique thus precious. To keep them safe, he wants to divide and store them in different locations. First, he has bought some coffers. These boxes are identical except that their capacities (the number of the diamonds that a box can contain) may diverse. Before he puts his diamonds in, he wants to figure out how many different ways he can store these diamonds into the coffers. He recruits you, a young programmer from Zhejiang University, to give him the answer.

 

输入

The input consists of several test cases. Each test case begins with a positive integer N, which indicates the number of the coffers Jack has bought. Then the following N integers are the capacities of each box. The total number of the diamonds Jack has equals to the sum of the capacities of all the coffers.

All the integers given are no greater than 12, you can be assured that a result always fits into a 32-bit integer.

 

输出

For every test case, print out an integer representing the number of the different ways Jack can store his diamonds. Each integer is to be printed on a single line.

 

样例输入

2
3 3
3
1 2 3

样例输出

10
60

 

#include<iostream>
#include<cstring>
using namespace std;
int cs[100][100];
void init()//排列组合初始化
{
    for(int i=0; i<=100; i++)
    {
        cs[i][0]=1;
        cs[i][i]=1;
    }
    for(int i=1; i<=100; i++)
        for(int j=1; j<i; j++)
        {
            cs[i][j]=cs[i-1][j-1]+cs[i-1][j];
        }
}
int jiecheng(int a)//阶乘
{
    int sum=1;
    for(int i=1; i<=a; i++)
        sum=sum*i;
    return sum;
}
int main()
{
    int n;
    int a[100];
    int b[100];

    init();
    while(cin>>n)
    {

        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int sum=0;
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
            sum=sum+a[i];
            b[a[i]]++;
        }
        int sum1=1;
        {
            for(int i=0; i<n; i++)
            {
                sum1=sum1*cs[sum][a[i]];
                sum-=a[i];
            }
        }
        int sum2=1;
        for(int i=0; i<=12; i++)
        {
            if(b[i]>=2)//如果有相同的需要除以相同的数的个数的阶乘。
            {
                sum2=sum2*jiecheng(b[i]);
            }
        }
        cout<<sum1/sum2<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值