P2265 路边的水沟

H y p e r l i n k Hyperlink Hyperlink

https://www.luogu.com.cn/problem/P2265


D e s c r i p t i o n Description Description

有一张 n × m n\times m n×m的网格图,求从右下走到左上的方案数

数据范围: n , m ≤ 1 0 6 n,m\leq 10^6 n,m106


S o l u t i o n Solution Solution

一共要走 n + m n+m n+m步,随便走 m m m步向上的即可,答案即为 C ( n + m , m ) C(n+m,m) C(n+m,m)


C o d e Code Code
#include<cstdio>
#include<cctype>
#define LL long long
#define N 1000000
#define mod 1000000007
using namespace std;
LL inv[N+10],fac[N+10],d[N+10],n,m,t;
inline LL read()
{
	LL d=1,f=0;char c;
	while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
	while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
	return d*f;
}
inline LL ksm(LL x,LL y)
{
	LL ans=1;
	for(;y;y>>=1,(x*=x)%=mod) if(y&1) (ans*=x)%=mod;
	return ans;
}
inline LL C(LL n,LL m){if(n<m) return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;}
signed main()
{
	fac[0]=fac[1]=inv[0]=inv[1]=1;
	for(register int i=2;i<=N;i++) fac[i]=fac[i-1]*i%mod;
	inv[N-1]=ksm(fac[N-1],mod-2);
	for(register int i=N-2;i>1;i--) inv[i]=(1ll*inv[i+1]*(i+1))%mod;
	n=read();m=read();
	printf("%lld",C(n+m,m));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值