版权声明:本文作者靖心。靖空间地址: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. 【spring】spring的事务传播性 hibernate/jpa等的事务隔离性

    spring的注解 @Trancational加在controller层,调用了service层的方法,service层的方法也加了@Trancational注解,这时候就出现了事务的嵌套,也就出现了 ...

  2. Linux sed 批量替换字符串和更多用法

    比如,要将目录/modules下面所有文件中的zhangsan都修改成lisi,这样做: # sed -i “s/zhangsan/lisi/g” `grep zhangsan -rl /module ...

  3. C# 中的结构类型(struct type)

    ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部   像类一样,结构(struct)是能够包 ...

  4. 如何查看linux版本 如何查看LINUX是多少位

    一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux ...

  5. Solr Cloud的搭建使用

    Solr的安装下载http://archive.apache.org/dist/lucene/solr/6.4.0/或者直接去官网下载最新版本网页指导 https://cwiki.apache.org ...

  6. java 环境变量配置 Mac

    大家在windows里面配置JDK环境变量很容易,但是如果要在mac里面配置JDK环境变量和windows里面有所不同,具体如下: 第一: mac OS里面自带jdk,不过是1.6的版本,现在很多人使 ...

  7. 微信小程序-使用腾讯Wxpage

    微信小程序想要更快的速度吗? 满足你 https://github.com/tvfe/wxpage#-c%E5%AE%9A%E4%B9%89 使用超简单(导入wxpage.js,最后使用对象名:P): ...

  8. JDK5新特性之线程同步工具类(三)

    一. Semaphore Semaphore能够控制同一时候訪问资源的线程个数, 比如: 实现一个文件同意的并发訪问数. Semaphore实现的功能就类似厕全部5个坑, 增加有十个人要上厕所, 那么 ...

  9. 图论 Krusal算法C++实现

    1.实验用例 如下图所示的赋权图表示某七个城市及预先算出它们之间的一些直接通信成路造价(单位:万元),试给出一个设计方案,使得各城市之间既能够通信又使总造价最小并计算其最小值. 2实验原理和方法 为了 ...

  10. hdu2141Can you find it?

     给你四个集合.要你从这四个集合中 各取出一个数出来,推断,取出的前三个数的和 是否等于第四个数. 数据比較大.我的做法是将 前两个集合全部数全部和的情况取出来, 然后二分查找第四个集合和第三集合 ...