Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 344 Accepted Submission(s): 236
Problem Description
車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。
现在要问问你,满足要求的方案数是多少。
现在要问问你,满足要求的方案数是多少。
Input
第一行一个正整数T,表示数据组数。
对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。
对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。
Output
对于每组数据输出一行,代表方案数模1000000007(1e9+7)。
Sample Input
1 1 1
Sample Output
1
Source
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6114
题意:分析一下,就是C(n,m),(n>m) ,利用组合数的递推关系或者用Lucas定理即可得到答案。
当然也可以用记忆话搜索。
AC代码1:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
* 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e3+5;
int c[maxn][maxn];
const int mod=1e9+7;
void Init()
{
memset(c,0,sizeof(c));
c[0][0]=c[1][0]=c[1][1]=1;
for(int i=2;i<maxn;i++)
{
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
{
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
}
}
int main()
{
//freopen("C:\\Users\\hncu_acm\\Desktop\\data.txt","r",stdin);
Init();
int T,n,m;
cin>>T;
while(T--)
{
cin>>n>>m;
if(n<m)
swap(n,m);
cout<<c[n][m]<<endl;
}
return 0;
}
AC代码2:
Lucas定理
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
* 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e1+5;
const int mod=1e9+7;
typedef long long LL;
LL qmod(LL a,int n,LL p)
{
LL ans=1;
while(n)
{
if(n&1)
ans=(ans*a)%p;
a=a*a%p;
n>>=1;
}
return ans;
}
LL Comb(LL n,LL m,LL p)
{
if(n<m) return 0;
if(n==m) return 1;
m = min(n - m,m);
LL ans=1,cn=1,cm=1;
for(LL i=0;i<m;i++)
{
cn=(cn*(n-i))%p;
cm=(cm*(m-i))%p;
}
ans=(cn*qmod(cm,p-2,p))%p;
return ans;
}
LL Lucas(LL n,LL m,LL p)
{
LL ans=1;
while(n&&m&&ans)
{
ans=(ans*Comb(n%p,m%p,p))%p;
n/=p;
m/=p;
}
return ans;
}
int main()
{
//freopen("C:\\Users\\hncu_acm\\Desktop\\data.txt","r",stdin);
int T,n,m;
cin>>T;
while(T--)
{
cin>>n>>m;
if(n<m)
swap(n,m);
cout<<Lucas(n,m,mod)<<endl;
}
return 0;
}