CF-weekly4 F. Kyoya and Colored Balls

F. 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.ios

Input

The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors.app

Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).ui

The total number of balls doesn't exceed 1000.this

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

Examples
input
Copy
3
2
2
1
output
Copy
3
input
Copy
4
1
2
3
4
output
Copy
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:code


1 2 1 2 3
1 1 2 2 3
2 1 1 2 3


題意:
一共有n種顏色的球,同種顏色的球視爲徹底相同,設置一種放置規則,即上一種顏色球的最後一個必須放在當前顏色球最後一個的前面,問放球的方案數。
題解:
把狀況分開考慮,一種顏色一種顏色的往上放,那麼dp[i]表示已經放了i種顏色的方案數,a[i]表示第i種顏色的球的個數,sum[i]表示前i種球一共有這麼多個,因此當放入i-1種顏色的球后,要把第i種球放一個到最後面以知足題意,而後向前插球,即當前一共有sum[i-1]+a[i]-1個位置,要在這些位置中找a[i]-1個位置放剩下的第i種球,那麼這種操做的方法數就是c[sum[i-1+a[i]-1]][a[i]-1]
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1010
#define mod 1000000007 
using namespace std;
int n,a[maxn],c[maxn][maxn],dp[maxn],sum[maxn];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum[i]=sum[i-1]+a[i];
    }
    c[0][0]=1;
    for(int i=1;i<=1000;i++){
        c[i][0]=1;
        for(int j=1;j<=1000;j++)
            c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
    }
    dp[0]=1;
    for(int i=1;i<=n;i++)
        dp[i]=1LL*dp[i-1]*c[a[i]-1+sum[i-1]][a[i]-1]%mod;
    printf("%d\n",dp[n]);
    return 0;
}
相關文章
相關標籤/搜索