O(N)求组合数

思路:打表后发现是杨辉三角

#include
#include
#include
#include
#define ll long long 
#define MAXN 200100
#define INF 0X3f3f3f3f
#include
#include
#include
#include
const ll mod=1000000007;
const ll inf=1e18;
using namespace std;
int n,m;

ll fac[MAXN]; 

ll pow(ll x, ll n, ll mod){
    ll res = 1;
    while(n){
        if(n&1){
            res = res * x % mod;
        }
        x = x*x % mod;
        n >>= 1;
    }
    return res;
}

void init(){
	fac[0]=1;
	for(ll i=1;i<=MAXN;i++){
		fac[i]=(fac[i-1]*i) % mod;
	}
//	memset(vis,false,sizeof(vis));
}

ll C(int x,int y){//组合数 = x!*(y!%p的逆元)*((x-y)!%mod的逆元)%mod
//	if(vis[x][y]) return C[x][y];
//	vis[x][y]=true;
	ll ans= (fac[x]*pow(fac[y],mod-2,mod)%mod * pow(fac[x-y],mod-2,mod)%mod )%mod;
 	return ans;
}


int main(){
	init();
	cin >> n >> m;
	cout << C(n+m-4,m-2);
}

你可能感兴趣的:(ACM暑期集训)