【BZOJ2886】最短路【组合数】

【题目链接】

同【BZOJ3260的题解】

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const ULL p = 1000000007;

void exgcd(ULL a, ULL b, LL &x, LL &y) {
	b ? (exgcd(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0);
}

ULL inv(ULL a) {
	LL x, y;
	exgcd(a, p, x, y);
	if(x < 0) x += p;
	return x % p;
}

int main() {
	ULL n, m; scanf("%llu%llu", &n, &m);
	if(m > n) swap(n, m); n += m + 1; n %= p;

	ULL a = 1, b = 1;
	for(int i = 1; i <= m; i++) {
		a = (n - i + 1) * a % p;
		b = i * b % p;
	}
	
	ULL ans = (a * inv(b) % p + n - m - 1) % p;
	printf("%llu\n", ans);

	return 0;
}


你可能感兴趣的:(【BZOJ2886】最短路【组合数】)