有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第nn行第mm列的格子有几种方案,答案对10000000071000000007取模。
把这个方格填数:
0 0 0 0 0 0
0 1 1 1 1 1
0 1 2 3 4 5
0 1 3 6 10 15
发现和杨辉三角形有很大关系
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
杨辉三角形公式:C(N-1,M-1)
例如本题中的3行4列是三角形中的4行二列即C(3,1)
故由推理可得C(N+M-4,N-2)就是本题中的题解。
组合数取模用到快速积、lucas定理、组合数取模等知识点
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 100050
#define MOD 1000000007
#define p 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
#define FOR(i , n) for(int i = 1 ; i<= n ; i ++)
typedef pair<int , int> pii;
const long long INF= 0x3fffffff;
LL n,m;
LL quick_mod(LL a, LL b)
{
LL ans = 1;
a %= p;
while(b)
{
if(b & 1)
{
ans = ans * a % p;
b--;
}
b >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL n, LL m)//求组合数加取模
{
if(m > n) return 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, p-2) % p) % p;
}
return ans;
}
LL Lucas(LL n, LL m)
{
if(m == 0) return 1;
return C(n % p, m % p) * Lucas(n / p, m / p) % p;//lucas定理
}
int main()
{
while(scanf("%I64d%I64d", &n, &m) != EOF)
{
if(n <= 1 || m <= 1)
{
printf("0\n");
continue;
}
//m -= 2;
n = n + m - 4;
m -= 2;
printf("%I64d\n", Lucas(n,m));
}
return 0;
}