HDU 6114 Chess【逆元+组合数】【模板题】

Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 95    Accepted Submission(s): 72


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
1
 

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define ms(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define ll long long

using namespace std;

const int mod = 1000000007;
const int maxn = 100010;

ll fast_pow(ll x, ll n)
{
	ll ans = 1;
	while (n)
	{
		if (n & 1) ans = (ans*x) % mod;
		x = (x*x) % mod;
		n >>= 1;
	}
	return ans;
}

ll inv(ll a, ll p)	//费马定理求a关于p的逆元
{
	return fast_pow(a, p - 2);
}

ll C(ll n, ll m)	//n取m
{
	ll ans = 1;
	for (ll i = n - m + 1; i <= n; i++) ans = (ans*i) % mod;
	for (ll i = 1; i <= m; i++) ans = (ans*inv(i, mod)) % mod;
	return ans;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		ll n, m;
		scanf("%I64d%I64d", &n, &m);
		printf("%I64d
", C(max(n, m), min(n, m)));
	}
	return 0;
}





Fighting~
原文地址:https://www.cnblogs.com/Archger/p/12774706.html