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\n");
        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\n", sum);
        }
    }
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11886510.html
今日推荐