codeforces 131c 组合数

来源:互联网 发布:前苏联十大实验知乎 编辑:程序博客网 时间:2024/05/22 09:04

题目:

C. The World is a Theatre
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different.

Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java.

Input

The only line of the input data contains three integers nmt (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m).

Output

Find the required number of ways.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.

Examples
input
5 2 5
output
10
input
4 3 5
output
3

利用组合数递推公式:C(n, m) = C(n - 1, m - 1) + C(n - 1, m) 对应最后一个选或者不选的情况


代码:

#include<bits/stdc++.h>using namespace std;const int maxn=70;///只开到35的话 20 20 40 这组样例会因为越界报错__int64 n,m,t;__int64 c[maxn][maxn];int main(){    memset(c,0,sizeof(c));    c[0][0]=1;    for(int i=1;i<=30;++i){        c[i][0]=c[i][i]=1;        for(int j=1;j<=i;++j){            c[i][j]=c[i-1][j-1]+c[i-1][j];        }    }    /*for(int i=0;i<=30;++i){        for(int j=0;j<=30;++j){            cout<<c[i][j]<<" ";        }        cout<<endl;    }*/    scanf("%I64d%I64d%I64d",&n,&m,&t);    __int64 ans=0;    for(__int64 i=4;i<t;++i){        ans+=c[n][i]*c[m][t-i];///若判一下i和n的关系以及m和t-1的关系数组就可以开小一些    }    printf("%I64d\n",ans);    return 0;}


也可以使用递推公式每次算一下,不过会慢一些:

#include<bits/stdc++.h>using namespace std;__int64 n,m,t;__int64 C(__int64 n,__int64 m){    __int64 ans=1;    for(__int64 i=1;i<=m;++i){        ans*=(n-i+1);        ans/=i;    }    return ans;}int main(){    scanf("%I64d%I64d%I64d",&n,&m,&t);    __int64 ans=0;    for(__int64 i=4;i<t;++i){        ans+=C(n,i)*C(m,t-i);    }    printf("%I64d\n",ans);    return 0;}



原创粉丝点击