Light OJ 1005 - Rooks 数学题解
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 数学题解的更多相关文章
- Light oj 1005 - Rooks (找规律)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. //#pragma comm ...
- Light OJ 1005 - Rooks(DP)
题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...
- (light OJ 1005) Rooks dp
http://www.lightoj.com/volume_showproblem.php?problem=1005 PDF (English) Statistics Forum Tim ...
- Light Oj 1005
题意: 从 n*n 的棋盘中放置 K 个 行和列不冲突的棋子 思路: 组合数学, 先选 k 个 行, k 个列, 就是 C(n,k) ^ 2; 然后 K 个棋子不相同, K ! 全排列 #includ ...
- 1005 - Rooks(规律)
1005 - Rooks PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ Dynamic Programming
免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
随机推荐
- 瞬发大量并发连接 造成MySQL连接不响应的分析
http://www.actionsky.com/docs/archives/252 2016年12月7日 黄炎 目录 1 现象 2 猜想 3 检查环境 4 猜想2 5 分析 5.1 TCP握手的 ...
- Geoserver跨域问题
1 Geoserver位war包,使用Tomcat发布的情况 由于geoserver为war形式,需要解决tomcat跨域问题,geoserver版本为2.9.1. (1)下载跨域war包 下载cor ...
- cocos2dx 2.x新建项目
举例: cocos2d-x-2.2.6/tools/project-creator 进入 这个文件夹 chmod 777 project-cereator.py ./create_project.py ...
- OpenCV头文件包含问题
opencv从2.2版本以后<opencv root>include下有两个文件夹 opencv 和opencv2.从官方的意思来看,它逐渐喜欢用opencv2里面的那种包含头文件的方式. ...
- docker入门小结(二)
11,网络使用 sudo docker run -d -P training/webapp python app.py sudo docker ps -l 这样将主机一个端口映射到容器中,由于app. ...
- Linux学习之十四-Linux文件和目录权限
Linux文件和目录权限 在Linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问这些文件和目录. 通过设定权限可以从以下三种访问方式限制访问权限:只允许用户自己访问:允 ...
- tomcat启动项目,起不起来
右键tomcat 选择publish
- HTML5 Canvas 绘制加拿大枫叶旗
这段代码比较简单,中间的枫叶使用了图片,因为没找到画法: <!DOCTYPE html> <html lang="utf-8"> <meta http ...
- Linux的经常使用命令(2) - 关机
关机命令 shutdown‑h now 马上进行关机 shutdown‑r now 如今又一次启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k : ...
- vuex mapGetters
1.vuex 配置 //vuex的配置 //注意Store是大写 const store = new Vuex.Store({ //数据保存 state: { show: false, count: ...