Persona5 is a famous video game.
In the game, you are going to build relationship with your friends.
You have N friends and each friends have his upper bound of relationship with you. Let's consider the ith friend has the upper bound Ui. At the beginning, the relationship with others are zero. In the game, each day you can select one person and increase the relationship with him by one. Notice that you can't select the person whose relationship with you has already reach its upper bound. If your relationship with others all reach the upper bound, the game ends.
It's obvious that the game will end at a fixed day regardless your everyday choices. Please calculate how many kinds of ways to end the game. Two ways are said to be different if and only if there exists one day you select the different friend in the two ways.
As the answer may be very large, you should output the answer mod 1000000007
Input Format
The input file contains several test cases, each of them as described below.
- The first line of the input contains one integers N(1≤N≤1000000), giving the number of friends you have.
- The second line contains N integers. The i^{th}ith integer represents (1≤Ui≤1000000), which means the upper bound with ith friend. It's guarantee that the sum of Ui is no more than 1000000.
There are no more than 10 test cases.
Output Format
One line per case, an integer indicates the answer mod 1000000007.
样例输入
3
1 1 1
3
1 2 3
样例输出
6
60
解题思路:
首先,根据题意不难推得计算公式,即和的阶乘除以阶乘的积。
直接计算一定超时。尝试过取对数计算,但数据量太大,同样超时。
转换思路,注意到(a+b+c+d)! / a! b! c! d! = C(a+b+c+d , a) (b+c+d)! / b! c! d! = C(a+b+c+d , a) C(b+c+d , b) (c+d)! / c! d! = ... =C(a+b+c+d , a) C(b+c+d , b) C(c+d , c) C(d , d)
原式可以写成组合数乘积的形式。
用Lucas定理分别计算各组合数,最后快速积取模即可求解。Lucas计算组合数通过费马小定理求逆元实现。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll Jc[1000002];
void calJc() //求maxn以内的数的阶乘
{
Jc[0] = Jc[1] = 1;
for(ll i = 2; i < 1000002; i++)
Jc[i] = Jc[i - 1] * i % mod;
}
ll pow(ll a, ll n, ll p) //快速幂 a^n % p
{
ll ans = 1;
while(n)
{
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
ll niYuan(ll a, ll b) //费马小定理求逆元
{
return pow(a, b - 2, b);
}
ll C(ll a, ll b) //计算C(a, b)
{
return Jc[a] * niYuan(Jc[b], mod) % mod
* niYuan(Jc[a - b], mod) % mod;
}
ll ModMul(ll a,ll b,ll n)//快速积取模 a*b%n
{
ll ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%n;
a=(a+a)%n;
b>>=1;
}
return ans;
}
ll Lucas(ll a, ll b)
{
if(a < mod && b < mod)
return C(a, b);
return
C(a % mod, b % mod) * Lucas(a / mod, b / mod);
}
int main()
{
int n;
calJc();
while(~scanf("%d",&n))
{
ll number;
ll i,sum=0,ans=1;
for(i=0;i<n;i++)
{
scanf("%lld",&number);
sum+=number;
ans=ModMul(ans,Lucas(sum,number),mod);
}
printf("%lld\n",ans);
}
return 0;
}