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,m≤106
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));
}