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 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
随机推荐
- Maven项目导入到Eclipse时Build出现the user operation is waiting for building workspace to complete的问题解决
解决办法如下: 1.选择菜单栏的[Project],然后把菜单栏中[Build Automatically]前面的对钩去掉.
- CPU、内存、IO虚拟化关键技术及其优化探索
https://yq.aliyun.com/articles/71295?spm=5176.8091938.0.0.3LQ7NM
- Android性能优化第(一)篇---基本概念
最近打算总结几篇app性能优化方面的东西,毕竟android弄了这么久,万一到哪些转了行,岁月久了就忘记了,纯粹当个人笔记.今个是第一篇---性能优化的基本概念,毛主席说了,让理论先行,理论指导实践. ...
- flask的安全注意事项,如何防范XSS、CSRF、JSON安全
参考官方文档:http://docs.jinkan.org/docs/flask/security.html 1.xss Flask 配置 Jinja2 自动转义所有值,除非显式地指明不转义.这就排除 ...
- nginx configure 错误记录
1.the HTTP rewrite module requires the PCRE library. ./configure: error: the HTTP rewrite module req ...
- apue学习笔记(第十三章 守护进程)
本章将说明守护进程结构,以及如何编写守护进程程序. 守护进程,也就是通常说的Daemon进程,是Unix中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理 ...
- code::blocks(版本号10.05) 配置opencv2.4.3
(1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 详细操作例如以下: 第一步, 配置compiler, 操作步骤为Settings -> Comp ...
- Python 的下载安装
学习Python牛逼的教程: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000,本文 ...
- HDU 4930 Fighting the Landlords(扯淡模拟题)
Fighting the Landlords 大意: 斗地主... . 分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black ...
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
再用爬虫爬取数据的时候报错:[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661) 好多博客我看都说是:网站证书 ...