C. 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
  1. 3
    2
    2
    1
output
  1. 3
input
  1. 4
    1
    2
    3
    4
output
  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
    1 1 2 2 3
    2 1 1 2 3

题意:有k种不同颜色的球,然后给出不同颜色的球的个数,把球排成一列,要求第i+1种颜色的球的最后一个一定要在第i种颜色的球的最后面一个的后面,求摆放的方案数mod1e9+7

题解:下标大的先放,对与每一种球它的最后一个球的位置是确定的,然后就是在剩下的位置中挑剩下的球的个数个位置C(sum-1,a[i]-1),然后相乘。关建是求通过乘法逆元组合数。

  1. #include<bits/stdc++.h>
  2. #define pb push_back
  3. #define ll long long
  4. #define PI 3.14159265
  5. using namespace std;
  6. const int maxn=1e3+;
  7. const int mod=1e9+;
  8. const int inf=0x3f3f3f3f;
  9. int n,sum;
  10. int a[maxn];
  11. ll b[(int)1e6+];
  12. ll ans=;
  13. ll poww(ll x,ll k)
  14. {
  15. ll t=;
  16. while(k)
  17. {
  18. if(k%)
  19. {
  20. t=(t*x)%mod;
  21. }
  22. x=(x*x)%mod;
  23. k/=;
  24. }
  25. return t;
  26. }
  27. ll c(ll x,ll y)//组合数公式
  28. {
  29. if(x<y)return ;
  30. if(y==)return ;
  31. ll t=;
  32. ll tmp=(b[x-y]*b[y])%mod;
  33. t=(b[x]*poww(tmp,mod-))%mod;//费马小定理求乘法逆元
  34. return t;
  35. }
  36. int main()
  37. {
  38. std::ios::sync_with_stdio(false);
  39. cin.tie();
  40. cout.tie();
  41. cin>>n;
  42. for(int i=;i<=n;i++)
  43. {
  44. cin>>a[i];
  45. sum+=a[i];
  46. }
  47. b[]=;b[]=;
  48. for(int i=;i<=sum;i++)
  49. {
  50. b[i]=(b[i-]*i)%mod;
  51. }
  52. for(int i=n;i>=;i--)
  53. {
  54. ll tmp=c(sum-,a[i]-);
  55. sum-=a[i];
  56. ans=(ans*tmp)%mod;
  57. }
  58. cout<<ans<<'\n';
  59. return ;
  60. }

Codeforces Round #309 (Div. 2)D的更多相关文章

  1. 贪心 Codeforces Round #309 (Div. 2) B. Ohana Cleans Up

    题目传送门 /* 题意:某几列的数字翻转,使得某些行全为1,求出最多能有几行 想了好久都没有思路,看了代码才知道不用蠢办法,匹配初始相同的行最多能有几对就好了,不必翻转 */ #include < ...

  2. 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

    题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...

  3. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  4. Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造

    B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

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

  6. Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题

    B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...

  7. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题

    A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

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

  9. Codeforces Round #309 (Div. 2)

    A. Kyoya and Photobooks Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ha ...

  10. Codeforces Round #309 (Div. 1) A(组合数学)

    题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...

随机推荐

  1. 两个input在同一行连着不留缝隙

    方法1:让两个input 连在一起写 不换行 <div class="inputDiv"> <input type="text" placeh ...

  2. 性能测试系列学习 day1

    性能测试的最终目标是为了最大限度的满足用户的需求,我们通常为了达到以下目标而进行性能测试: (1)评估系统的能力:测试中得到的负荷和响应时间数据可以被用于验证所计划的模型的能力,并帮助作出决策: (2 ...

  3. Java:用Lambda表达式简化代码一例

    之前,调用第3方服务,每个方法都差不多“长”这样, 写起来啰嗦, 改起来麻烦, 还容易改漏. public void authorizeRoleToUser(Long userId, List< ...

  4. CSS中的路径裁剪样式clip-path

    前面的话 CSS借鉴了SVG裁剪的概念,设置了clip-path样式,本文将详细介绍路径裁剪clip-path 概述 clip-path属性可以防止部分元素通过定义的剪切区域来显示,仅通过显示的特殊区 ...

  5. 为什么Java 两个Integer 中1000==1000为false而100==100为true?

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt346 这是一个挺有意思的讨论话题. 如果你运行下面的代码 1 2 3 4 I ...

  6. error: The requested URL returned error: 401 Unauthorized while accessing

    我遇到的其中一个问题. 问题描述: 在git push -u origin master是,提示“error: The requested URL returned error: 401 Unauth ...

  7. sublime text3 3143 注册码

    sublime text3  3143 注册码,可用~ Sublime Text一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制.相比于难于上手的Vi ...

  8. datable 翻页事件处理

    JQuery datatable插件,点下一页在点击事件无效问题 (2013-10-16 16:01:54) 转载▼   分类: C# 在MVC的项目中,我利用jquery datatable 来实现 ...

  9. mysql初学,mysql修改,mysql查找,mysql删除,mysql基本命令

    Mysql 下载地址https://dev.mysql.com/downloads/mysql/ 1.连接Mysql格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYS ...

  10. 团队作业1——团队展示&选题

    Deadline: 2017-4-5 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目包括后文的四个方面 团队博客 团队选题 团队计划 团队成员贡献分分配规则 晚交 - 0 ...