zoukankan      html  css  js  c++  java
  • A. Kyoya and Colored Balls_排列组合,组合数

    Codeforces Round #309 (Div. 1)

    A. Kyoya and Colored Balls
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color ibefore drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen.

    Input

    The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors.

    Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).

    The total number of balls doesn't exceed 1000.

    Output

    A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007.

    Examples
    input
    3
    2
    2
    1
    output
    3
    input
    4
    1
    2
    3
    4
    output
    1680
    Note

    In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are:


    1 2 1 2 3
    1 1 2 2 3
    2 1 1 2 3

    解题报告:

    1、可以从后往前思考,先把第n种颜色的,最后一个球放到最后,然后将这个颜色的其余的球随便放,然后将第(n-1)种颜色的球放到,之前放的球的最前一个的前面,递推下去。

    2、递推公式:

    for(int i=n;i>=1;i--)
    {
        if(cnt[i]==0) continue;
        ans=(ans*C[c-1][cnt[i]-])% mod;
        c-=cnt[i];
    }

    3、组合数递推公式:

    void init()
    {
        memset(C, 0, sizeof(C));
        C[0][0] = 1;
        C[1][0] = C[1][1] = 1;
        for(int i = 2; i <= 1000; ++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]) % mod;
            }
        }
    }
    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    
    const int maxn = 1010;
    const ll  mod  = 1000000007;
    ll C[maxn][maxn];
    
    void init()
    {
        memset(C, 0, sizeof(C));
        C[0][0] = 1;
        C[1][0] = C[1][1] = 1;
        for(int i = 2; i <= 1000; ++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]) % mod;
            }
        }
    }
    
    int cnt[maxn];
    
    int main()
    {
    
        init();
        int n;
        int c = 0;
        ll  ans = 1;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &cnt[i]);
            c += cnt[i];
        }
    
        for(int i = n; i >= 1; i--)
        {
            if(cnt[i] == 0) continue;
            ans = (ans * C[c-1][cnt[i]-1]) % mod;
            c -= cnt[i];
        }
    
        cout << ans << endl;
    
        return 0;
    }
  • 相关阅读:
    python日志设置[logging]
    python异常处理
    python可迭代对象、迭代器、生成器
    python字典操作
    python列表和元组的操作
    python字符串操作
    python深拷贝和浅拷贝
    python时间和日期的处理
    ssh
    SSH安全外壳协议
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5346542.html
Copyright © 2011-2022 走看看