Problem Description
有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第$n$行第$m$列的格子有几种方案,答案对$1000000007$取模。
Input
多组测试数据。 两个整数$n,m(2\leq n,m\leq 100000)$
Output
一个整数表示答案
Sample Input
4 5
Sample Output
10
推出来是这个式子:/*C(n+m-4,m-2)*/,表示不会推
代码:
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
const int p=1000000007;
LL quick_mod(LL n,LL m)
{
LL ans=1;
n=n%p;
while(m)
{
if(m&1)
{
ans=ans*n%p;
m--;
}
m>>=1;
n=n*n%p;
}
return ans;
}
LL C(LL n, LL m) //求组合C(n,m)
{
if(m > n) return 0; //n<m, c(n,m)=0
LL ans = 1;
for(int i=1; i<=m; i++)
{
LL a = (n + i - m) % p;
LL b = i % p;
ans = ans * (a * quick_mod(b, LL(p-2)) % p) % p;
}
return ans;
}
LL Lucas(LL n, LL m) //lucass定理
{
if(m == 0) return 1;
return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
int main()
{
int n,m;
long long ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=Lucas(n+m-4,m-2);
printf("%lld\n",ans);
}
return 0;
}