题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5698
思路来源:http://blog.youkuaiyun.com/qwb492859377/article/details/51478117
AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
const int maxn = 100010;
LL JC[maxn];
void createTable() {
JC[0] = 1;
for(int i = 1; i < maxn; i++) {
JC[i] = (i*JC[i-1])%mod;
}
}
LL resMod(LL x,LL pow) {
LL ans,res;
ans = 1;
res = x;
while(pow) {
if(pow&1) {
ans = (ans*res)%mod;
}
res = (res*res)%mod;
pow = pow>>1;
}
return ans;
}
LL C(int n,int m) {
LL temp = (JC[m]*JC[n-m])%mod;
//求temp的逆元,由于mod是素数,因此可以用小费马定理求逆元
LL resTemp = resMod(temp,mod-2);
LL ans = (JC[n]*resTemp)%mod;
return ans;
}
int main() {
int N,M;
createTable();
while(~scanf("%d%d",&N,&M)) {
int Min = min(N,M);
int Max = max(N,M);
LL ans = 0;
for(int i = 0; i <= Min-2; i++) {
ans += C(Min-2,i)*C(Max-2,i);
ans = ans%mod;
}
printf("%lld\n",ans);
}
return 0;
}