BZOJ 3260: 跳 (组合恒等式)
题意
题解
先走长的,再走短的一定最优。
也就是说,假设,行进路线为
然后第一行答案为
第二行就是
显然的组合恒等式
其中为较小值,为较大值。可以直接算了。因为有,所以
时间复杂度
CODE
#pragma GCC optimize ("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
int inv[MAXN];
inline int C(LL n, LL m) { n %= mod;
LL re = 1; inv[0] = inv[1] = 1;
for(int i = 2; i <= m; ++i) inv[i] = 1ll * (mod - mod/i) * inv[mod%i] % mod, re = re * inv[i] % mod;
for(int i = 1; i <= m; ++i) re = re * (n-i+1) % mod;
return re;
}
int main () { LL n, m;
scanf("%lld%lld", &n, &m);
printf("%lld\n", (max(n, m) + C(n+m+1, min(n, m))) % mod); //C(n+m+1,max(n+1,m+1))
}