机器人走方格 51Nod - 1118

组合数学+费马小定理...emmmmm

其实影响到达(n,m)这个点走法就是,什么时候选择什么时候向下走,一共要走 n-1+m-1步,从里面抽出n-1为向下走。

于是我们可以得到  s!/( (s-n)! *n! )%mod。

而    a=k*mod+c.....1式; b=k*mod+1..... 2式 

     1式*2式= a*b=k*mod+c;

   H^(m-1)=1mod m(m为素数)。

   令(s-n)!=H1,n!=H2     =>     s!/(s-n)!/n!%mod=s!* H1^(m-2)   *H2^(m-2);

AC node

#include<iostream>
#include<climits>
#include<cstring>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
const int M=2*1e6+5;
LL fac[M];
LL fast_power(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
LL combination(LL s,LL n)
{
    LL ans;
    ans=fac[s]%mod;
    ans=ans*fast_power(fac[n],mod-2)%mod;
    ans=ans*fast_power(fac[s-n],mod-2)%mod;
    return ans;
}
int main()
{
    LL n,m;
    LL ans;
    while(cin>>n>>m)
    {
        fac[0]=1;
        for(int i=1;i<=n+m;++i)
        fac[i]=fac[i-1]*i%mod;
        ans=combination(n-1+m-1,n-1);
        cout<<ans<<endl;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值