kuangbin 数学训练二 Rooks
题目链接:传送门#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;const int N = 50;ll t, n, k, ca, ans, c[50][50];//阶乘公式ll m
·
题目链接:
传送门
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 50;
ll t, n, k, ca, ans, c[50][50];
//阶乘公式
ll mul(ll n) {
ll tmp = 1;
for(ll i = 1; i <= n; i++) tmp *= i;
return tmp;
}
int main() {
//预处理组合数
for (int i = 0; i < N; i ++ ) {
for (int j = 0; j <= i; j ++ ) {
if (!j) {
c[i][j] = 1;
} else {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
}
}
scanf("%lld", &t);
while(t--) {
scanf("%lld%lld", &n, &k);
if(k > n) printf("Case %lld: 0\n", ++ca);
else {
//套公式
ans = c[n][k] * c[n][k] * mul(k);
printf("Case %lld: %lld\n", ++ca, ans);
}
}
}
这是一道排列组合题,大致题意是在n*n的棋盘上放k个棋子,且棋子与棋子之间不能同行或同列问有多少放法。
想法就是首先从n行中选k行,然后从k行中选k列,然后经行一个全排列即可得到答案。
更多推荐
所有评论(0)