• 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;
    }
  • 相关阅读:
    Linux之Permission denied没有权限
    soapUI的简单使用(webservice接口功能测试)
    jmeter学习(二),如何安装jmeter?
    loadrunner检查点设置失败,日志中SaveCount无法被正常统计出来
    loadrunner破解出现“license security violation,Operation is not allowed”的错误提示
    安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误
    IOS测试,打不开要测试的APP怎么办?设置信任
    Jmeter的好搭档Badboy的安装与简单使用
    映射网络驱动器会自动断开的解决方法
    oracle中如何修改用户名和密码
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5346542.html
Copyright © 2020-2023  润新知