思路:
写个dp式算出较小方格的,打个表。在纸上模拟画方格,在方格里面填数,根据方格里的数写出排列组合,推出组合公式:C(n - 1 + m - 1, n - 1),再用逆元处理下。
#include<cstdio>
#include<cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
LL q_mod(LL a, LL b) {
LL ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % mod;
a = ((a % mod) * (a % mod)) % mod;
b >>= 1;
}
return ans;
}
int main() {
LL n, m, p1, p2;
scanf("%lld %lld", &p1, &p2);
m = max(p1, p2);
n = min(p1, p2);
LL k1 = m + n - 2, ans1 = 1;
for(int i = 1; i <= n - 1; i++) {
ans1 = ((ans1 % mod) * k1) % mod;
k1--;
}
LL k2 = n - 1, ans2 = 1;
for(int i = 1; i < n - 1; i++) {
ans2 = ((ans2 % mod) * k2) % mod;
k2--;
}
LL ans3 = q_mod(ans2, mod - 2);
printf("%lld\n", ans1 * ans3 % mod);
return 0;
}