LightOJ - 1005 - Rooks(组合数)
Posted ydddd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LightOJ - 1005 - Rooks(组合数)相关的知识,希望对你有一定的参考价值。
链接:
https://vjudge.net/problem/LightOJ-1005
题意:
A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for rook R1 from its current position. The figure also shows that the rook R1 and R2 are in attacking positions where R1 and R3 are not. R2 and R3 are also in non-attacking positions.
Now, given two numbers n and k, your job is to determine the number of ways one can put k rooks on an n x n chessboard so that no two of them are in attacking positions.
思路:
只需考虑k行,组合数选k。
(C_n^k*prod_n^{n-k+1})
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 5e6+10;
const int MOD = 1e9+7;
int n, k;
int main()
{
int cnt = 0;
int t;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%d %d", &n, &k);
if (k > n)
printf(" 0
");
else
{
LL sum = 1;
for (int i = n;i > n-k;i--)
sum *= i;
for (int i = 1;i <= k;i++)
sum /= i;
for (int i = n;i > n-k;i--)
sum *= i;
printf(" %lld
", sum);
}
}
return 0;
}
以上是关于LightOJ - 1005 - Rooks(组合数)的主要内容,如果未能解决你的问题,请参考以下文章