51nod1119 机器人走方格 V2

终于学到了求组合数的正确姿势

//C(n+m-2,m-1)
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ll long long
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
}
const int nmax=2e6+5;
const ll mod=1e9+7;
ll a[nmax];
ll pow(ll x,int n){
	ll ans=x;--n;
	while(n){
		if(n&1) ans=(ans*x)%mod;
		x=(x*x)%mod;n>>=1;
	}
	return ans;
}
int main(){
	int n=read(),m=read();a[0]=1;
	rep(i,1,n+m-2) a[i]=a[i-1]*i%mod;
	printf("%lld
",a[n+m-2]*pow(a[n-1],mod-2)%mod*pow(a[m-1],mod-2)%mod);
	return 0;
}

  

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
M * N的方格,一个机器人从左上走到右下,只能向右或向下走。有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果。
 
Input
第1行,2个数M,N,中间用空格隔开。(2 <= m,n <= 1000000)
Output
输出走法的数量 Mod 10^9 + 7。
Input示例
2 3
Output示例
3
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5862663.html