51nod 1627 瞬间移动【组合数学】

来源:互联网 发布:asm算法原理 编辑:程序博客网 时间:2024/05/20 18:20

Description

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

题解

枚举走的步数,直接搞。

代码

#include<cstdio>#include<cstring>#include<algorithm>#define tt 1000000007#define maxn 100006#define LL long longusing namespace std;int n,m;LL ans,f[maxn*2],inv[maxn*2];LL power(LL x,int y){    if(!y)return 1;    if(y==1)return x%tt;    LL c=power(x,y>>1);    if(y&1)return c*x%tt*c%tt;      else return c*c%tt;}LL C(int x,int y){return f[x]*inv[x-y]%tt*inv[y]%tt;}int main(){    freopen("teleport.in","r",stdin);    freopen("teleport.out","w",stdout);    scanf("%d%d",&n,&m);    f[0]=1;inv[0]=1;    for(int i=1;i<=n+m;i++)f[i]=f[i-1]*i%tt,inv[i]=power(f[i],tt-2);    for(int i=0;i<=min(n,m)-2;i++)(ans+=C(n-2,i)*C(m-2,i)%tt)%=tt;    printf("%lld\n",ans);    return 0;}
原创粉丝点击