组合数学 逆元 模板

题目描述

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 U[i]. 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 

输入

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 ith integer represents U[i]( 1 ≤ U[i]≤ 1000000), which means the upper bound with ith friend. It’s guarantee that the sum of U[i] is no more than 1000000. 
There are no more than 10 test cases. 

输出

One line per case, an integer indicates the answer mod 1000000007. 

样例输入

3
1 1 1
3
1 2 3

样例输出

6
60
这个是组合数的问题,先预处理,然后逆元就好
运行错误那么多次...
#include <bits/stdc++.h>
const int maxn=1e6+5;
const int mod=1e9+7;
#define ll long long
using namespace std;
int n;
ll f[maxn],g[maxn];
ll powmod(ll x,int n)
{
    ll s=1;
    while(n){
        if(n&1)
            s=(s*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return s;
}
//ll C(ll n,ll m)
//{
//    ll a=f[n];
//    ll b=(f[m]*f[n-m])%mod;
//    return (a*powmod(b,mod-2))%mod;
//}
int main(){
    ll ans,sum;
    f[0]=1;
    for(int i=1;i<maxn;i++){
        f[i]=(f[i-1]*i)%mod;
    }
    g[maxn-1]=powmod(f[maxn-1],mod-2);
    for(int i=maxn-2;i>=0;i--){
        g[i]=g[i+1]*(i+1)%mod;
    }
    while(scanf("%d",&n)!=EOF){
    sum=0;
    int i,a;
    ans=1;
    for(i=1;i<=n;i++){
        scanf("%d",&a);
        ans=ans*g[a]%mod;
        sum+=a;
    }
    ans=ans*f[sum]%mod;
    cout<<ans<<endl;
    }
    return 0;
}
View Code

 

 

 

posted @ 2018-06-05 12:36  house_cat  阅读(223)  评论(0编辑  收藏  举报