Codeforences #309 C(div2)

来源:互联网 发布:电子模拟仿真软件 编辑:程序博客网 时间:2024/05/07 04:53

Kyoya Ootori has a bag with n colored balls that are colored withk different colors. The colors are labeled from1 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 colori 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 ci, the number of balls of thei-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, modulo1 000 000 007.

Examples
Input
3221
Output
3
Input
41234
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 31 1 2 2 3

2 1 1 2 3

【题意】给了n个球,其中n个球中共有k种颜色,现在要用n个球形成一些排列,a[i]表示颜色为i的有a[i]个,num之和为n,现在问你这n个小球有多少种排列方式,满足第i种颜色最后一个球后面的球的颜色是i+1。

【分析】先考虑第k种颜色,那么一定有一个颜色为k的小球放在最后。

【状态表示】dp[i]表示前i种颜色的方法总数。

【状态转移】dp[i+1] = dp[i]*(C[sum(i+1)-1][a[i+1]-1]);

【说明】C[i][j]表示组合数,可以提前预处理,sum[i+1]表示2前i+1种小球颜色种数,a[i+1]代表颜色为i+1的小球个数。

【参考博客】http://blog.csdn.net/kirito_acmer/article/details/46830331

【AC代码】

#include <bits/stdc++.h>using namespace std;typedef long long ll;#define mod 1000000007const int N = 1002;ll C[N][N];ll dp[N];void init(){    for(int i=1; i<=1000; i++) C[i][0]=1;    for(int i=1; i<=1000; i++)    {        for(int j=1; j<=i; j++)        {            if(i==j)C[i][j]=1;            else                C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;        }    }}int main(){    int n,a[1005];    ios::sync_with_stdio();cin.tie(0);    init();    cin>>n;    for(int i=1; i<=n; i++)cin>>a[i];    ll sum=a[1],ans=1;    for(int i=2; i<=n; i++)    {        sum = sum + a[i];        ans = ((ans%mod)*(C[sum-1][a[i]-1]%mod))%mod;    }    cout<<ans<<endl;    return 0;}


0 0
原创粉丝点击