CSU-2049 象棋

CSU-2049 象棋

Description

車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。
现在要问问你,满足要求的方案数是多少 。

Input

第一行一个正整数T,表示数据组数。( T<=10)
对于每组数据:一行,两个正整数N和M(N<=100000,M<=100000)。

Output

对于每组数据输出一行,代表方案数模1000000007(10^9+7)。

Sample Input

1
1 1

Sample Output

1

题解

又是一个数学题,考虑(mgeq n)的情况(对于(m leq n)的情况可以转化为(m ge n), 旋转棋盘即可),每行只能放一个车,即为(C_m ^n),从m列中选出n列,这样选出的列自动从小到大依次摆在每一行即可

要用乘法逆元

#include<bits/stdc++.h>
#define maxn 100050
using namespace std;
const long long p = 1e9 + 7;
long long fac[maxn];
long long fast_pow(long long a, int b) {
	long long ans = 1;
	while (b) {
		if (b & 1)
			ans = ans * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return ans % p;
}
long long C(int m, int n) {
	return fac[n] * fast_pow(fac[m], p - 2) % p * fast_pow(fac[n - m], p - 2) % p;
}
int main() {
	int t;
	scanf("%d", &t);
	fac[0] = 1;
	for (int i = 1; i <= maxn; i++) {
		fac[i] = fac[i - 1] * i % p;
	}
	while (t--) {
		int n, m;
		scanf("%d%d", &n, &m);
		if (m > n) swap(n, m);
		printf("%d
", C(m, n));
	}
}
原文地址:https://www.cnblogs.com/artoriax/p/10349137.html