BZOJ 3260: 跳 (组合恒等式)

题意

传送门

题解

先走长的,再走短的一定最优。

也就是说,假设n>mn>m,行进路线为(0,0)(1,0)...(n1,0)(n,0)(n,1)...(n,m)(0,0)\to(1,0)\to...\to(n-1,0)\\\to(n,0)\to(n,1)\to...\to(n,m)

然后第一行答案为nn

第二行就是i=0m(n+in)\sum_{i=0}^m \binom{n+i}{n}

显然的组合恒等式i=0m(n+in)=(n+m+1n+1)=(n+m+1m)\sum_{i=0}^m \binom{n+i}{n}=\binom{n+m+1}{n+1}=\binom{n+m+1}m

其中mm为较小值,nn为较大值。可以直接算了。因为有nm<=1012n*m<=10^{12},所以m<=106m<=10^6

时间复杂度O(min(n,m))=O(106)O(min(n,m))=O(10^6)

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))
}
posted @ 2019-12-14 14:50  _Ark  阅读(138)  评论(0编辑  收藏  举报