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

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

时间: 2024-11-08 21:28:32

LightOJ - 1005 - Rooks(组合数)的相关文章

Lightoj 1005 Rooks(DP)

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 fig

LightOJ 1005 Rooks(组合排列)或(dp,还得再看看)

n*n的棋盘中,放k个棋子,每个棋子不能同行同列 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<string> 5 #include<math.h> 6 #include<stack> 7 #include<cstdlib> 8 #include<set> 9 #include<map> 10 #includ

1005 - Rooks(规律)

1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB 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 positio

Light oj 1005 - Rooks (找规律)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <c

Light OJ 1005 - Rooks(DP)

题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这个推导过程如下. 当我们们第n*n的矩阵的时候可以考虑第(n-1)*(n-1)的矩阵经过哪些变换可以变成n*n的. 如上图蓝色方格.我们加入蓝色方格之后,矩阵就会增大一圈. 1.加入我们蓝色方格不放置棋子. dp[n-1][k] 2.加入蓝色方格放置一枚棋子,那么我们其实有三种位置可以放置:(1)右

LightOJ-1005 组合数学,组合数水题

LightOJ 1005 题意:n*n的棋盘,放入k个车,要使k个车不相互攻击,有多少种方案. 总结:从n行选出k行(C(n,k)),再从n列选出k列(A(n,k)),即C(n,k)*A(n,k). 注:纠结是C还是A,举个简单的例子,看交换后是否相同. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define F(i,

Light OJ Dynamic Programming

免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一个字符串i 到jLCS为k的方案数 1068 - Investigation 数位dp 能被K整数且各位数字之和也能被K整除的数 dp[i][j][k] 到第i位每位数字之和的余数为j 当前数字余数为k 1079 - Just another Robbery 01背包 全部钱之和为背包体积 不被抓的

(light OJ 1005) Rooks dp

http://www.lightoj.com/volume_showproblem.php?problem=1005    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB 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 vert

lightoj 1095 - Arrange the Numbers(dp+组合数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 dp[i]=(i-1)*dp[i-2](表示新加的那个数放到i-1中的某一个位置然后那个被放位置的数放在i这个位置就是i-2的错排)+(i-1)*dp[i-1](表示新加的那个数放到i-1中的某一个位置然后用那个位置被占的数代替i这个位置的数就是i-1的错排) #include <iostream