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

Source: Internet
Author: User

Kyoya Ootori have a bag withNColored balls that is colored withkdifferent colors. The colors is labeled from1Tok. Balls of the same color is indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of colorIBefore drawing the last ball of color i? +?1For allIFrom1To k?-? 1. Now he wonders how many different ways this can happen.

Input

The first line of input would have one integer k (1?≤? K≤?1000) The number of colors.

Then,kLines would follow. TheI-th line would contain Ci , the number of balls of theI-th Color (1?≤? C i? ≤?1000).

The total number of balls doesn ' t exceed 1000.

Output

A single Integer, the number of ways this Kyoya can draw the balls from the bag as described in the statement, modulo 1?000?000?007.

Sample Test (s) input
3221
Output
3
Input
41234
Output
1680
Note

In the first sample, we had 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya is:

1 2 1 2 31 1 2 2 3

2 1 1) 2 3

This problem let me learn the calculation of combinatorial number, because the direct use of the combination of number formula will lead to inaccurate results, such as C (100,50), if you multiply a number in a number method, it may result in an error can not be divided evenly.

Idea: If the total number of methods of the first I color is f (i), then the total number of methods of i+1 color is f (i+1) =f (i) *c (sum (i+1) -1,a[i+1]-1), where sum (i+1) is the sum of the number of previous i+1 colors.

#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <map > #include <algorithm>using namespace std; #define LL __int64#define maxn 1000000007int a[1600];ll c[1050][1060] ; ll Sum;int main () {int n,m,i,j,sum1;for (i=1;i<=1000;i++) c[i][0]=1;for (i=1;i<=1000;i++) {for (j=1;j<=i;j++) {if (i==j) C[i][j]=1;else if (i>j) c[i][j]= (c[i-1][j]+c[i-1][j-1])%maxn;}} while (scanf ("%d", &n)!=eof) {for (i=1;i<=n;i++) {scanf ("%d", &a[i]);} Sum1=a[1];sum=1;for (i=2;i<=n;i++) {sum1+=a[i];//printf ("%d%d\n", a[i]-1,sum1-1); sum= (Sum*c[sum1-1][a[i]-1])% Maxn;//sum= (Sum*f (a[i]-1,sum1-1))%maxn;//printf ("%lld\n", sum);} printf ("%i64d\n", sum);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.