uva 10105 uva 10910 uva 10943(排列组合C)

来源:互联网 发布:一路向前知乎 编辑:程序博客网 时间:2024/06/06 08:30

10105:

题意:

给ni,k,求  (x1+x2+...+xk)n.展开式中x1n1x2n2...xknk. 的系数。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int maxn = 1e6;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);LL C(int m, int n){    LL res = 1;    if (n - m < m)        m = n - m;    for (int i = 1; i <= m; i++)    {        res = res * (n - i + 1) / i;    }    return res;}int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    #endif // LOCAL    int n, k;    while (scanf("%d%d", &n, &k) == 2)    {        LL ans = 1;        for (int i = 1; i <= k; i++)        {            int x;            scanf("%d", &x);            ans *= C(x, n);            n -= x;        }        printf("%lld\n", ans);    }    return 0;}

10910:

题意:

n门考试,得总分t分,每门及格分为p。

求在及格的情况下得分的情况总数。


解析:

n个盒子,放m = t - n×p颗球,盒子可空,求放法。

插板法,共n-1+m个位置插板,取n-1个。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int maxn = 1e6;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);LL C(LL n, LL m){    LL res = 1;    if (n - m < m)        m = n - m;    for (LL i = 1; i <= m; i++)    {        res = res * (n - i + 1) / i;    }    return res;}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    int ncase;    scanf("%d", &ncase);    while (ncase--)    {        LL n, t, p;        scanf("%lld%lld%lld", &n, &t, &p);        t = t - n * p;        printf("%lld\n", C(n - 1 + t, n - 1));    }    return 0;}

10943:

题意:

求k个小于n的非负整数相加等于n共有几种情况。


解析:

隔板法。

n个球放到k个盒子里,盒子可空。共n + k - 1个板,取k - 1个板。

因为最后要取模,所以不能像上题一样在除法中取模,所以用C的递归形式。

C[ i ] [ 0 ] = 1, C[ n ] [ m ] = C[ n - 1 ] [ m - 1 ] + C[ n - 1 ] [ m ].


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int maxn = 200 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);//LL C(LL n, LL m)//{//    LL res = 1;//    if (n - m < m)//        m = n - m;//    for (LL i = 1; i <= m; i++)//    {//        res = res * (n - i + 1)  / i;//    }//    return res;//}const int mod = 1000000;LL C[maxn][maxn];void c_table(){    memset(C, 0, sizeof(C));    for (int i = 0; i <= 200; i++)    {        C[i][0] = 1;    }    for (int i = 1; i <= 200; i++)    {        for (int j = 1; j <= i; j++)        {            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;        }    }}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    LL n, k;    c_table();    while (~scanf("%lld%lld", &n, &k))    {        if (!n && !k)            break;//        printf("%lld\n", C(n + k - 1, k - 1) % 1000000);        printf("%lld\n", C[n + k - 1][k - 1]);    }    return 0;}



代码:


0 0
原创粉丝点击