版权声明:本文作者靖心。靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/31760795

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.

Input

Input starts with an integer T (≤ 350), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 30) and k (0 ≤ k ≤ n2).

Output

For each case, print the case number and total number of ways one can put the given number of rooks on a chessboard of the given size so that no two of them are in attacking positions. You may safely assume that this number will be less than 1017.

Sample Input

Output for Sample Input

8

1 1

2 1

3 1

4 1

4 2

4 3

4 4

4 5

Case 1: 1

Case 2: 4

Case 3: 9

Case 4: 16

Case 5: 72

Case 6: 96

Case 7: 24

Case 8: 0

本题有点像n-queen的问题的变形,只是和n-queen有本质的不同,由于简化了对角线,那么就能够使用数学的方法求解了。

思考了我好久,最终发现这个是inclusion-exclusion原理的应用。

1 当k等于0的时候为1。 当k等于1的时候,那么就等于n^2

2 能够这样选择的:先选择n^2中的一格,那么就剩下(n-1)^2格能够选择了,然后在选一格。那么又剩下(n-2)^2格选择了

3 这样能够利用乘法原理得到f(n, k) = f(n^2, 1) * f((n-1)^2, 1) * f((n-2)^2, 1)...*f((n-k+1)^2, 1);

4 相当于分别在n, n-1, n-2... (n-k+1)个方格中分别选择一格。

5 可是这样选择有反复。由于选择出来的数不须要排序,那么就把其排序的方法的次数除去,这是依据除法原理计算法

6 最终得到:f(n, k) = f(n^2, 1) * f((n-1)^2, 1) * f((n-2)^2, 1)...*f((n-k+1)^2, 1) / P(k); P(k)是k个数的全排序

比如求f(4, 3) = f(4, 1) * f(3, 1) * f (2,, 1) / 3!;

f(4, 4) = f(4, 1), *f(3, 1), *f(2, 1) / 4!;

由于f(3, 3) = f(3, 1) * f (2, 1) / 3!;

所以能够化简:f(4, 4) = f(3, 3) * f(4, 1) / 4; 最后就利用这个公式,加上动态规划法,能够先计算出一个表,然后直接查表得到答案,速度奇快。

#pragma once
#include <stdio.h>
#include <vector>
using namespace std; class Rooks1005
{
const static int SIZE = 31;
vector<vector<long long> > tbl; void genTbl()
{
for (int i = 1; i < SIZE; i++)
{
tbl[i][0] = 1;
tbl[i][1] = i * i;
} for (int i = 2; i < SIZE; i++)
{
for (int j = 2; j <= i; j++)
{
tbl[i][j] = tbl[i][1] * tbl[i-1][j-1] / j;
}
}
}
public:
Rooks1005():tbl(SIZE, vector<long long>(SIZE))
{
genTbl(); int T, n, k;
scanf("%d", &T);
for (int i = 1; i <= T; i++)
{
scanf("%d %d", &n, &k);
if (k > n) printf("Case %d: %d\n", i, 0);
else printf("Case %d: %lld\n", i, tbl[n][k]);
}
}
};

Light OJ 1005 - Rooks 数学题解的更多相关文章

  1. Light oj 1005 - Rooks (找规律)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. //#pragma comm ...

  2. Light OJ 1005 - Rooks(DP)

    题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...

  3. (light OJ 1005) Rooks dp

    http://www.lightoj.com/volume_showproblem.php?problem=1005        PDF (English) Statistics Forum Tim ...

  4. Light Oj 1005

    题意: 从 n*n 的棋盘中放置 K 个 行和列不冲突的棋子 思路: 组合数学, 先选 k 个 行, k 个列, 就是 C(n,k) ^ 2; 然后 K 个棋子不相同, K ! 全排列 #includ ...

  5. 1005 - Rooks(规律)

    1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...

  6. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  7. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  8. Light OJ Dynamic Programming

    免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...

  9. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

随机推荐

  1. 聊聊、Zookeeper Windows启动

    Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly rel ...

  2. Checkbox Text 重影问题的解决的方法

    Checkbox有个属性值 <CheckBox android:id="@+id/cb_reg_agree" style="@style/reg_checkbox_ ...

  3. bat文件转换为exe文件

    批处理文件转换为exe文件(简单的处理文件),点击下载 使用超简单的了,不多说.

  4. Scanner遇上UnmappableCharacterException

    上周末的时候.朋友约好去KTV,鉴于我这样的不怎么听歌的孩子伤不起啊,灵机一动就把我的酷狗歌单导出来了,XML文件嘛,内容太多,我仅仅想要歌名足已. 于是写了一个java去输出歌名.     岂料我受 ...

  5. jq:正则表达式

    var checkNum = /^[A-Za-z0-9]+$/;   注意没有引号 checkNum.test(这里添加待匹配的字符串); var textNull=/^\s*$/; if(textN ...

  6. 跨域用ajax处理并返回处理状态

    <script type="text/javascript">        $(function () {            $("#submitBtu ...

  7. React学习之事件绑定

    React事件绑定有主要有三种方式 第一种官方推荐方式: class LoginControl extends React.Component {   constructor(props) {     ...

  8. iOS 一些struct类型的NSLog输出

    我们经常会输出一些坐标尺寸信息之类的,比如view的frame,是CGRect类型的,用frame.oringial.x 和frame.size.width来做NSLog参数好麻烦,还好苹果对这些常用 ...

  9. 转jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用

    JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...

  10. Arrays.asList引起的java.lang.UnsupportedOperationException解决方法

    在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...