zoukankan      html  css  js  c++  java
  • Remainders Game

    Remainders Game

    Problem:

    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 i before 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 c i, the number of balls of the i-th color (1 ≤ c i ≤ 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
    

    Solution:

    组合数学。题意是求保证(i)种颜色气球的最后一个之后必须是(i+1)种颜色的气球的情况下的排列数。对于每一种颜色计算当前所剩的总位数(sum),当前颜色的个数(a)(C[sum-1][a-1]),将所有情况相乘即可。

    预先处理组合数,递推方法计算:

    [c[i][j]=c[i-1][j]+c[i-1][j-1] \(杨辉三角求法) ]

    Code:

    #include <bits/stdc++.h>
    #define lowbit(x) (x&(-x))
    #define CSE(x,y) memset(x,y,sizeof(x))
    #define INF 0x3f3f3f3f
    #define Abs(x) (x>=0?x:(-x))
    #define FAST ios::sync_with_stdio(false);cin.tie(0);
    using namespace std;
    
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    
    const int maxn = 1111;
    const ll mod = 1000000007;
    ll c[maxn][maxn];
    int a[maxn];
    
    void Ini()
    {
    	c[0][0] = 1; c[1][0] = 1; c[1][1] = 1;
    	for (int i = 2; i < maxn; i++) {
    		c[i][0] = c[i][i] = 1;
    		for (int j = 1; j < maxn; j++) {
    			c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    			c[i][j] %= mod;
    		}
    	}
    	return;
    }
    
    int main()
    {
    	FAST;
    	Ini();
    	int k, sum = 0;
    	cin >> k;
    	for (int i = 1; i <= k; i++) {
    		cin >> a[i];
    		sum += a[i];
    	}
    	ll ans = 1;
    	for (int i = k; i >= 1; i--) {
    		ans *= c[sum - 1][a[i] - 1];
    		ans %= mod;
    		sum -= a[i];
    	}
    	cout << ans << endl;
    	return 0;
    }
    
  • 相关阅读:
    Archlinux笔记本安装手记
    linux下activemq安装与配置activemq-5.15.2
    在 CentOS7 上安装 Zookeeper-3.4.9 服务
    VMware虚拟化kvm安装部署总结
    打印机故障总结
    fluentd安装和配置,收集docker日志
    使用Python和AWK两种方式实现文本处理的长拼接案例
    MySQL数据库使用xtrabackup备份实现小例子
    shell脚本实现ftp上传下载文件
    Linux系统中创建大文件,并作为文件系统使用
  • 原文地址:https://www.cnblogs.com/LeafLove/p/13331496.html
Copyright © 2011-2022 走看看