链接: http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=776&pid=1001
Chess Accepts: 1805 Submissions: 5738
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。
现在要问问你,满足要求的方案数是多少。
Input
第一行一个正整数T,表示数据组数。
对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。
Output
对于每组数据输出一行,代表方案数模1000000007(1e9+7)。
Sample Input
1
1 1
Sample Output
Copy
1
分析:组合数 C(n,m)
一直TLE,没想到与逆元结合,其实还是挺简单的。。。还是渣 由组合数的公式,还要取模,大数除法取模,就是逆元了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int N=1e3+5;
typedef long long LL;
LL fac[N],inv[N];
LL pow_mod(LL a,LL b)
{
LL ans=1;
a%=mod;
while(b)
{
if(b&1) ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
void init()
{
inv[0]=fac[0]=1;
for(int i=1;i<N;i++)
{
fac[i]=fac[i-1]*i%mod;
inv[i]=pow_mod(fac[i],mod-2);///inv数组为对应的逆元
}
}
int main()
{
int t;
init();
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n<m) swap(n,m);///n始终为大的那个
//printf("fac[%d]=%d fac[%d]=%d\n",n,fac[n],m,fac[m]);
printf("%I64d\n",fac[n]*inv[m]%mod*inv[n-m]%mod);
}
return 0;
}