Ural 1114. Boxes 解题报告(组合数计算)

1114. Boxes

Time limit: 0.6 second
Memory limit: 64 MB
N boxes are lined up in a sequence (1 ≤  N ≤ 20). You have  A red balls and  B blue balls (0 ≤  A ≤ 15, 0 ≤  B≤ 15). The red balls (and the blue ones) are exactly the same. You can place the balls in the boxes. It is allowed to put in a box, balls of the two kinds, or only from one kind. You can also leave some of the boxes empty. It's not necessary to place all the balls in the boxes. Write a program, which finds the number of different ways to place the balls in the boxes in the described way.

Input

Input contains one line with three integers  NA and  B separated by space.

Output

The result of your program must be an integer written on the only line of output.

Sample

input output
2 1 1
9
Problem Source: First competition for selecting the Bulgarian IOI team.
Tags: none   ( hide tags for unsolved problems )

    解题报告: 挺坑的一题。早上做的时候提交了一发"%lld",一发"%llu",都是G++模式,结果都跪了。我就开始怀疑是不是理解错题意了 = =

    刚才忍不住看了别人的解题报告,一般人都是DP做的,大家可以百度。不过他们的输出都是cout。

    我把之前的代码改成cout,结果就A了。我又用"%I64u"试了一下,又A了。。。G++也支持I64u,佩服呀。

    好吧,以后不知道的话直接用cout = =

    网上看到的都是DP,我直接用乘法解决了,很是神奇呀。让我们将a个红球,b个蓝球分配到n个盒子里。从红球的角度看,如果不使用红球,情况数为1。如果用1个红球,情况数为C(n, 1)。使用2个红球,情况数为C(n+1, 2)...一直递推即可。蓝球也是这样计算。最后相乘即可。代码如下:

#include 
#include 
#include 
#include 
using namespace std;
typedef unsigned long long LL;

int main()
{
    int n,a,b,c;
    scanf("%d%d%d",&n,&a,&b);
    c=max(a,b);
    n--;

    LL ans = 1, tmp = 1, sum = 1;
    for(int i=1;i<=c;i++)
    {
        tmp = tmp*(n+i)/i;
        sum += tmp;

        if (i==a) ans *= sum;
        if (i==b) ans *= sum;
    }

    printf("%I64u\n", ans);
}


你可能感兴趣的:(ACM,数学,数论)