BZOJ 2339 [HNOI2011]卡农(组合计数)

orz GXZlegend的博客

讲的好!!

CODE

#include 
using namespace std;
const int MAXN = 1000005;
const int mod = 100000007;
int n, m, P[MAXN], f[MAXN];
inline int qmul(int a, int b) {
	int res = 1;
	while(b) {
		if(b&1) res = 1ll * res * a % mod;
		a = 1ll * a * a % mod; b >>= 1;
	}
	return res;
}
int main () {
	scanf("%d%d", &n, &m);
	int k = qmul(2, n) - 1, fac = 1; P[0] = 1;
	for(int i = 1; i <= m; ++i) P[i] = 1ll * P[i-1] * (k-i+1) % mod, fac = 1ll * fac * i % mod;
	f[0] = 1; f[1] = 0;
	for(int i = 2; i <= m; ++i) f[i] = (P[i-1] - f[i-1] - 1ll * f[i-2] * (i-1) % mod * (k-(i-2)) % mod) % mod;
	printf("%lld\n", (1ll * f[m] * qmul(fac, mod-2) % mod + mod) % mod);
}

你可能感兴趣的:(其它DP,组合)