版权声明:本文作者靖心。靖空间地址: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. php类中静态变量与常亮的区别

    在效率上:常量编译过程比静态变量快的多. 代码: <?php error_reporting(E_ALL); class A { const c = 9; public static $b = ...

  2. android(cm11)状态栏源代码分析(一)

    (一):写在前面 近期因为工作须要,须要了解CM11中的有关于StatusBar相关的内容.总的来说,刚開始阅读其源代码的时候,是有点困难,只是通过构建相关代码的脑图和流程图,几天下来.我已经对其源代 ...

  3. metaq入门部署到实战

    初识metaq zookeeper部署,这里单机zk为例. wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.5/zookeep ...

  4. jq:正则表达式

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

  5. Spring2.5学习3.2_编码剖析@Resource注解的实现原理

    首先看一下J2EE提供的@Resource注解:该注解默认安照名称进行装配,名称能够通过name属性进行指定, 假设没有指定name属性,当注解写在字段上时,默认取字段名进行依照名称查找,假设注解写在 ...

  6. 实用国际(XX)计量单位表

    很多实用附录简表:http://www.zdic.net/appendix/f1.htm 计量单位简表 时间的单位换算 : 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)  1秒=1,00 ...

  7. Angular 一些问题(跨域,后台接收不到参数)

    1,跨域:跟前端没多大关系的,后台没设置头而已.这时候如果你们后端太菜你可以叫他加上每种语言 都不同,但是里面的呢荣是一样的.具体跨域可以跳转这里http://www.cnblogs.com/dojo ...

  8. mycat可以干什么

     单纯的读写分离,此时配置最为简单,支持读写分离,主从切换 分表分库,对于超过 1000 万的表进行分片,最大支持 1000 亿的单表分片 多租户应用,每个应用一个库,但应用程序只连接 Myca ...

  9. 集群通信组件Tribes之怎样维护集群成员信息

    一个集群包括若干成员,要对这些成员进行管理就必需要有一张包括全部成员的列表.当要对某个节点做操作时通过这个列表能够准确找到该节点的地址进而对该节点发送操作消息.怎样维护这张包括全部成员的列表是本节要讨 ...

  10. 开发ActiveX控件调用另一个ActiveX系列3——ActiveX调用另一个ActiveX

    终于进入正题了,怎样在ActiveX中调用另一个ActiveX.我们的项目需要调用华视电子身份证识别仪的ActiveX控件 在这里有很多识别仪ActiveX插件下载:http://www.idukaq ...