我是靠谱客的博主 沉默月亮,最近开发中收集的这篇文章主要介绍51nod 1120 机器人走方格V3 卡特兰数+卢卡斯定理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

点这里

忘了好久的公式。。。终于想起来查


卡特兰数




卡特兰数计算公式C(2n-2,n-1)/n或C(2n,n)-C(2n,n-1)


卢卡斯定理 用于大组合数取模运算




ans=(C(2n-2,n-1)/n)%mod

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef long long ll;
const int maxn= 10010;
const int mod=10007;
int  f[10020];
void init()
{
          f[0]=1;
    for(int i=1;i<=maxn;i++)
        f[i]=(i*f[i-1])%mod;

}

long long pow1(long long  n,long long m )
{
    long long ans = 1;
    while(m > 0)
    {
        if(m & 1)ans = (ans * n) % mod;
        m = m >> 1;
        n = (n * n) % mod;}
    return ans;
}
long long lucas(ll n,ll m)
{
     ll ans=1;
     while(n&&m)
     {
         ll x,y;
         x=n%mod;y=m%mod;
         if(x<y)
           return 0;
           //printf("%lld %lld %d %d %dn",x,y,f[x],f[y],f[x-y]);
         ans=ans*f[x]*pow1(f[y]*f[x-y]%mod,mod-2)%mod;
         n/=mod;m/=mod;
     }
    return ans%mod;
}
int main()
{   init();
    ll n,m,t;
    //freopen("out.txt","w",stdout);
while(~scanf("%lld",&n))
{
    ll t=pow1(n,mod-2);
     printf("%lldn",2*lucas(2*n-2,n-1)*t%mod);
}
    return 0;
}


最后

以上就是沉默月亮为你收集整理的51nod 1120 机器人走方格V3 卡特兰数+卢卡斯定理的全部内容,希望文章能够帮你解决51nod 1120 机器人走方格V3 卡特兰数+卢卡斯定理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部