码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ - 1005 - Rooks(组合数)

时间:2019-11-19 01:07:10      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:mes   game   mat   play   turn   for   --   组合数   show   

链接:

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;
}

LightOJ - 1005 - Rooks(组合数)

标签:mes   game   mat   play   turn   for   --   组合数   show   

原文地址:https://www.cnblogs.com/YDDDD/p/11886510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!