数学_组合数学+Lucas定理+卡特兰数

题意:

N * N的方格,从左上到右下画一条线。一个机器人从左上走到右下,只能向右或向下走。并要求只能在这条线的上面或下面走,不能穿越这条线,有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10007的结果。

思路:

卡特兰数 + Lucas定理
此题就是一个模板题

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const ll mod = 10007;
ll f[maxn];
void init(){
	f[0] = 1;
	for(ll i = 1;i <= mod;i++){
		f[i] = (f[i - 1] * i) % mod;
	}
}
ll pow(ll a,ll b){
	ll ans = 1;
	a = a % mod;
	while(b){
		if(b & 1) ans = (ans * a) % mod;
		b >>= 1;
		a = (a * a) % mod;
	}
	return ans % mod;
}
ll inv(ll x){
	 return pow(x,mod - 2);
}
ll C(ll n,ll m){
	if(m > n) return 0;
	 return f[n] * inv(f[m] * f[n - m]) % mod;
}
ll Lucas(ll n,ll m){
	if(m == 0) return 1;
	return C(n%mod,m%mod)*Lucas(n/mod,m/mod)%mod;
}
int main(){
	ll n;
	scanf("%lld",&n);
	n--;
	init();
	ll ans1 = Lucas(n * 2,n);//Lucas定理
	ll ans2 = Lucas(n * 2,n - 1);//Lucas定理
	ll ans = ((ans1 - ans2 + mod) % mod + (ans1 - ans2 + mod) % mod) % mod;//卡特兰数内容
	printf("%lld\n",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值