Remainders Game

Remainders Game

Problem:

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

3
2
2
1

output

3

input

4
1
2
3
4

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

Solution:

组合数学。题意是求保证\(i\)种颜色气球的最后一个之后必须是\(i+1\)种颜色的气球的情况下的排列数。对于每一种颜色计算当前所剩的总位数\(sum\),当前颜色的个数\(a\)\(C[sum-1][a-1]\),将所有情况相乘即可。

预先处理组合数,递推方法计算:

\[c[i][j]=c[i-1][j]+c[i-1][j-1] \\(杨辉三角求法) \]

Code:

#include 
#define lowbit(x) (x&(-x))
#define CSE(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define Abs(x) (x>=0?x:(-x))
#define FAST ios::sync_with_stdio(false);cin.tie(0);
using namespace std;

typedef long long ll;
typedef pair pii;
typedef pair pll;

const int maxn = 1111;
const ll mod = 1000000007;
ll c[maxn][maxn];
int a[maxn];

void Ini()
{
	c[0][0] = 1; c[1][0] = 1; c[1][1] = 1;
	for (int i = 2; i < maxn; i++) {
		c[i][0] = c[i][i] = 1;
		for (int j = 1; j < maxn; j++) {
			c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
			c[i][j] %= mod;
		}
	}
	return;
}

int main()
{
	FAST;
	Ini();
	int k, sum = 0;
	cin >> k;
	for (int i = 1; i <= k; i++) {
		cin >> a[i];
		sum += a[i];
	}
	ll ans = 1;
	for (int i = k; i >= 1; i--) {
		ans *= c[sum - 1][a[i] - 1];
		ans %= mod;
		sum -= a[i];
	}
	cout << ans << endl;
	return 0;
}

你可能感兴趣的:(Remainders Game)