题目描述:

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 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

Copy

  1. 3
  2. 2
  3. 2
  4. 1

Output

Copy

  1. 3

Input

Copy

  1. 4
  2. 1
  3. 2
  4. 3
  5. 4

Output

Copy

  1. 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. 1 2 1 2 3
  2. 1 1 2 2 3
  3. 2 1 1 2 3

思路:

题目是说给一组有颜色的球,从袋子中去出球要求第i种颜色的求必须在第i+1种颜色的求取完之前取完,问这种取球方法有多少种。大致可以看出这是一道排列组合题,而且方案会很多(因为要取模)。一开始想的是整体怎么放,就是说我一下子就要先扣下每种颜色的一个球,固定住他们的顺序,然后在看其他的球的放法。但情况实际上十分复杂。然后想的是这是一种有重复元素的定序排列问题,但直接套公式好像又不可行。应该要分步考虑而不是全局考虑。考虑最后一个位子,肯定放最后一种颜色的球,之前的位置有\(sum-1\)个,剩余的最后颜色球放在这些位子上有\(C_{sum-1}^{a[last]-1}\)种放法(同种颜色的球无差别)。然后考虑倒数第二种颜色的最后一个球,这是忽略掉前面放好的球,只看空位,最后一个空位放一个球,其它空位放剩余倒数第二种颜色的球,有\(C_{sum-a[last]-1}^{a[last-1]-1}\)种放法。以此类推直到第一种颜色的球。

注意在实现组合数时用到了费马小定理求逆元来算组合数取模。

代码

  1. #include <iostream>
  2. #define max_n 1005
  3. #define mod 1000000007
  4. using namespace std;
  5. int n;
  6. long long a[max_n];
  7. long long ans = 1;
  8. long long sum = 0;
  9. long long q_mod(long long a,long long b)
  10. {
  11. long long res = 1;
  12. while(b)
  13. {
  14. if(b&1)
  15. {
  16. res = ((res%mod)*a)%mod;
  17. }
  18. a = (a*a)%mod;
  19. b >>= 1;
  20. }
  21. return res;
  22. }
  23. long long fac[max_n];
  24. void ini()
  25. {
  26. fac[0] = 1;
  27. for(int i = 1;i<max_n;i++)
  28. {
  29. fac[i] = ((fac[i-1]%mod)*i)%mod;
  30. }
  31. }
  32. long long inv(long long a)
  33. {
  34. return q_mod(a,mod-2);
  35. }
  36. long long comb(int n,int k)
  37. {
  38. if(k>n) return 0;
  39. return (fac[n]*inv(fac[k])%mod*inv(fac[n-k])%mod)%mod;
  40. }
  41. int main()
  42. {
  43. ini();
  44. //cout << comb(3,1) << endl;
  45. cin >> n;
  46. for(int i = 0;i<n;i++)
  47. {
  48. cin >> a[i];
  49. sum += a[i];
  50. }
  51. for(int i = n-1;i>=0;i--)
  52. {
  53. ans = (ans%mod*(comb(sum-1,a[i]-1)%mod))%mod;
  54. sum -= a[i];
  55. }
  56. cout << ans << endl;
  57. return 0;
  58. }

参考文章:

hellohelloC,CodeForces 553A Kyoya and Colored Balls (排列组合),https://blog.csdn.net/hellohelloc/article/details/47811913

Codeforces A. Kyoya and Colored Balls(分步组合)的更多相关文章

  1. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. codeforces 553A . Kyoya and Colored Balls 组合数学

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  3. C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))

    C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...

  4. codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)

    题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...

  5. A. Kyoya and Colored Balls_排列组合,组合数

    Codeforces Round #309 (Div. 1) A. Kyoya and Colored Balls time limit per test 2 seconds memory limit ...

  6. CF-weekly4 F. Kyoya and Colored Balls

    https://codeforces.com/gym/253910/problem/F F. Kyoya and Colored Balls time limit per test 2 seconds ...

  7. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  8. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. react-native项目如何在xcode上打开ios项目

    如何打开ios项目? 导入或者双击ios/thirtydays.xcodeproj

  2. IBM X3650 m4 面板指示灯

  3. Windows彻底卸载VMWare虚拟机详细步骤

    不能卸载vmware ,原因是VMware的服务在运行中,停止服务就可以卸载了. 点击开始输入[services.msc],然后点击搜索到服务. 找到这个软件的图一的所有项,然后右键它属性. 全部设置 ...

  4. python mysqldb批量执行语句executemany

    MySQLdb提供了两个执行语句的方法,一个是execute(),另一个是executemany() execute(sql) 可接受一条语句从而执行 executemany(templet,args ...

  5. 配置 Jenkins 连接 Kubernetes 集群

    需求:外部 Jenkins 需要连接 Rancher 中的 Kubernetes 集群. 1.集群 config 文件 Rancher 首页,"集群" --> 右上角&quo ...

  6. c# 数据请求方式提供

    营销平台数据请求介绍 项目介绍: 前端使用 WPF,采用MVVM模式  后端数据库采用的sqlite 依靠本地化运行   后期可能会采用WebApi   因为WPF都是自学的 所以 代码方面写的可能不 ...

  7. setInterval的使用

    可以通过设置标识,判断方法是否执行完 var interval = setInterval(function () { if( flag > 0 ){ clearInterval(interva ...

  8. python selenium爬虫工具

    今天seo的同事需要一个简单的爬虫工具, 根据一个url地址,抓取改页面的a连接,然后进入a连接里面的页面再次抓取a连接 1.需要一个全局的set([])集合来保存抓取的url地址 2.由于现在单页面 ...

  9. Prometheus 基于文件的服务发现

    Prometheus 基于文件的服务发现 官方文档:https://github.com/prometheus/prometheus/tree/master/discovery 服务发现支持: end ...

  10. sqlserver分布式事务

    启动服务中的Distributed Transaction Coodinator后 创建链接服务器ender-pc\subx 设定连接服务器RPC OUT 以及RPC属性为True 实验一下代码 创建 ...