逆元求组合数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Linfanty/article/details/79948686
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
#define show(a) cout<<#a<<" = "<<a<<endl
#define show2(b,c) cout<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl
#define show3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl
#define show4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl
const int maxn = 100005;
#define LOCAL
const ll mod = 1000000007;

ll jie[maxn+1];
ll in[maxn+1];

ll poww(ll a, ll b) {
	if( b < 0 ) return 0;
    ll ret = 1;  
    a %= mod;  
    while(b) {  
        if (b & 1) ret = (ret * a) % mod;  
        b >>= 1;  
        a = (a * a) % mod;  
    }  
    return ret;  
}

ll inv(ll x) {
	return poww(x, mod - 2);
}

void init() {
	jie[0] = 1;
	for(int i = 1; i <= maxn; i++)
		jie[i] = jie[i-1] * i % mod;

	in[maxn] = inv(jie[maxn]) % mod;
	for(int i = maxn; i > 0; i--)
		in[i-1] = in[i] * i % mod; // (i-1)!'s inv ************
}

ll C(ll a, ll b) { // a * inv(b) % mod;
	if( a < 0 || b > a) return 0;
	// show3(jie[a], in[b], in[a-b]); in[0]=1
	return jie[a] * in[b] % mod * in[a - b] % mod;

	// inv(b!)*inv((a-b)!)
}


int main() {
    C(a, b)
    
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页