51nod-1627 瞬间移动(组合数+逆元)

题目描述:

有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几种方案,答案对1000000007取模。

Input
单组测试数据。
两个整数n,m(2<=n,m<=100000)
Output
一个整数表示答案。
Input示例
4 5
Output示例
10

题目分析:定义f(n,m)为从左上角走到(n,m)的所有方案数。显然有f(n,m)=∑ ∑ f(i,j),其中1≤i<n、j≤1<m。这是组合数。

代码如下:
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>

//#define _COMPLIE
#ifdef _COMPLIE
#include "51nod_1627.h"
#endif // _COMPLIE

using namespace std;

typedef long long LL;
const int mod=1000000007;
const int N=200000;

class nod_1627
{
private:
    int n,m;
    LL c[N+5];
    void getC(int x);
    LL myPow(LL a,int x);
public:
    void input();
    void process();
    void output();
};

void nod_1627::input()
{
    scanf("%d%d",&n,&m);
}

LL nod_1627::myPow(LL a,int x)
{
    LL res=1;
    while(x){
        if(x&1)
            res=(res*a)%mod;
        a=(a*a)%mod;
        x>>=1;
    }
    return res;

}

void nod_1627::getC(int x)
{
    c[0]=c[x]=1;
    c[1]=c[x-1]=x;
    for(int i=2;i<=x/2;++i){
        c[x-i]=c[i]=(((c[i-1]*(LL)(x-i+1))%mod)*myPow(i,mod-2))%mod;
    }
}

void nod_1627::process()
{
    --n,--m;
    getC(n+m-2);
}

void nod_1627::output()
{
    printf("%lld\n",c[m-1]);
}


int main()
{
    //freopen("in.txt","r",stdin);
    nod_1627 problem;
    problem.input();
    problem.process();
    problem.output();
    return 0;
}

 

转载于:https://www.cnblogs.com/20143605--pcx/p/6402792.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值