题意:已知有n个男生,m个女生。现在要选t个人,要求有至少4个男生,至少1个女生,求有多少种选法。
分析:
1、展开,将分子中的m!与分母中n!相约,即可推出函数C。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1e5 + 10; const int MAXT = 10000 + 10; using namespace std; LL C(LL n, LL m){ LL ans = 1; for(LL i = 1; i <= m; ++i){ ans *= n - i + 1; ans /= i; } return ans; } int main(){ LL n, m, t; while(scanf("%I64d%I64d%I64d", &n, &m, &t) == 3){ LL ans = 0; for(LL i = 4; i < t; ++i){ ans += C(n, i) * C(m, t - i); } printf("%I64d\n", ans); } return 0; }
2、递推求组合数。
高中学的组合数公式:C(n, m) = C(n - 1, m - 1) + C(n - 1, m)。
注意m <= n。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 30 + 10; const int MAXT = 10000 + 10; using namespace std; LL c[MAXN][MAXN]; void init(){ 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]; } } } int main(){ LL n, m, t; init(); while(scanf("%I64d%I64d%I64d", &n, &m, &t) == 3){ LL ans = 0; for(LL i = 4; i < t; ++i){ if(t - i <= m && i <= n) ans += c[n][i] * c[m][t - i]; } printf("%I64d\n", ans); } return 0; }