Time Limit: 1 second(s) Memory Limit: 32 MB

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

 
 
 
 
dp[n][k] 可以由三种状态转化而来:
1.  第 n 层不放象棋  dp[n-1][k]
2.  第 n 层放一个象棋, 可以放象棋的位置为图中蓝色的位置,即(2*(i-j)+1)*dp[i-1][j-1]
3.  第 n 层放两个象棋, 可以放象棋的位置为图中浅蓝色的位置 (i-j+1)*(i-j+1)*dp[i-1][j-2]
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define N 1100 #define met(a,b) (memset(a,b,sizeof(a)))
typedef long long LL; LL dp[N][N]; ///dp[n][k] 代表前n*n的矩阵中,放k个象棋的方法数 void Init()
{
int i, j; for(i=; i<=; i++)
dp[i][] = ; for(i=; i<=; i++)
{
for(j=; j<=i; j++)
{
dp[i][j] += dp[i-][j];
dp[i][j] += (*(i-j)+)*dp[i-][j-];
if(j>=)
dp[i][j] += (i-j+)*(i-j+)*dp[i-][j-];
}
} } int main()
{
int T, iCase=;
scanf("%d", &T); Init();
while(T--)
{
int n, k; scanf("%d%d", &n, &k); printf("Case %d: %lld\n", iCase++, dp[n][k]);
}
return ;
}

(light OJ 1005) Rooks dp的更多相关文章

  1. (期望)A Dangerous Maze(Light OJ 1027)

    http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...

  2. (状压) Brush (IV) (Light OJ 1018)

    http://www.lightoj.com/volume_showproblem.php?problem=1018   Mubashwir returned home from the contes ...

  3. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  4. (light oj 1319) Monkey Tradition 中国剩余定理(CRT)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...

  5. (light oj 1024) Eid (最小公倍数)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1024 In a strange planet there are n races. ...

  6. Light OJ 1005 - Rooks(DP)

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

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

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

  8. Light OJ 1005 - Rooks 数学题解

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

  9. 1002 - Country Roads(light oj)

    1002 - Country Roads I am going to my home. There are many cities and many bi-directional roads betw ...

随机推荐

  1. mac 使用技巧 (实时更新)

    一.部分快捷键. 这个在网上有一大堆,不过笔者认为,还是应该看个人习惯和使用环境吧.如果用得顺手,那就是快捷键,如果用得不顺手,那应该称其为“辅助键”吧. 下面介绍几个常用的快捷键: 1.截图. 屏幕 ...

  2. lintcode-【中等】数飞机

    题目: 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例: 对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], ...

  3. 2016-08-16: copy-and-swap

    #include <algorithm> // std::copy #include <cstddef> // std::size_t #include <stdio.h ...

  4. Ubuntu上安装MongoDB(译)

    add by zhj:直接从第四步开始就可以了,而且安装好MongoDB后会自动启动的,不必自己去执行启动命令 原文:https://docs.mongodb.com/manual/tutorial/ ...

  5. 深入理解js——执行上下文

    什么是"执行上下文"?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时,已经知道 ...

  6. 获取 input 单选框和多选框的值

    引用  jQuery的js <script> $(function(){ var arr = new Array(); $('#checkbox').click(function(){ a ...

  7. win 7安装 linux

    http://blog.csdn.net/wuwenxiang91322/article/details/23528619

  8. 第1章 C#类型基础

    1.1值类型和引用类型 1.1.1 值类型 使用值类型之前需要对值类型的所有元素初始化(普通值类型和结构体). 结构还有一个特性:调用结构上的方法前,需要对其所有的字段进行赋值,为了避免对结构体中所有 ...

  9. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

  10. 奇怪的margin,padding,table

    为什么有的时候margin,padding不管用?写了float以后就管用了? 为什么table 不给width,就默认是100%,里面的td会平均分配teble的宽度,若你想给其中一些td宽度,剩下 ...