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;
    }
  • 相关阅读:
    微信开发:微信js_sdk分享,使用场景,网页在微信app内部分享时的标题与描述,包括logo设置(一)
    云服务最开始的初始密码与远程连接密码?
    阿里云域名的ssl证书申请与腾讯服务器域名的证书安装
    关于 https的SNI问题
    关于网页授权access_token和普通access_token的区别
    转载:敏捷开发框架的优势
    select自定义下拉三角符号,css样式小细节
    关于ffmpeg /iis 8.5 服务器下,视频截取第一帧参数配置
    C# WebAPI中使用Swagger
    面向对象编程思想(OOP)
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5346542.html
Copyright © 2011-2022 走看看