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. vs 2013 Express 无法启动程序xxx.exe,系统找不到指定文件

    由于实验室有人用了含病毒的软件,网管把实验室出口给封了,周末人家又不上班.看样子树莓派是玩不成了,所以昨天在宿舍写windows程序,最基本的窗口程序,听说这段代码初学者至少要自己敲5遍以上.代码如下 ...

  2. Python从题目中学习:List comprehension

    九九乘法表作业其实有更简单的做法,就是用列表推导式. ------------------------------------------------------------------------- ...

  3. review过去的10年

    本科毕业有10个年头多了,如果对我的博客做一个主题分析,还真能发现一些规律,这里总结一下: 1.  活跃度 本科毕业最后一学期是思维最活跃的阶段,人生面临很多的变化和挑战,心态相对还不错. 从来北京以 ...

  4. jquery tmpl遍历

    最近发现大家用模板渲染一些顺带逻辑功能代码块时,用jquery tmpl较多,遇到了一些问题,现在就个人以前研究过的一切常用功能作介绍,主要针对遍历,其它的大家可以自行浏览一起网站,如:http:// ...

  5. C++学习基础三——迭代器基础

    迭代器分为两种:一种是iterator,另一种是const_iterator.两者都可进行访问容器中的元素,不同之处是:(1)const_iterator类型只能用于读取容器内的元素,不能更改其值:而 ...

  6. Mac下导出chrome插件

    chrome最强大的功能之一就是插件,有时候需要给小伙伴们共享一些插件,所以需要将自己chrome中的插件打包,在mac下打包插件还是挺费劲的,在此记录. 打开chrome的扩展程序,找到要导出的插件 ...

  7. 单链表在不知头结点的情况下对第i个元素的删除

    一.首先,看看单链表中第i个元素的删除: Status ListDelete_L (LinkList &L,int i,ElemType &e){ //在带头结点的单链表L中,删除第i ...

  8. IMS Global Learning Tools Interoperability™ Implementation Guide

    Final Version 1.1 Date Issued:            13 March 2012 Latest version:         http://www.imsglobal ...

  9. flex 导出Excel功能实现

    方法一: 1.Excel导出主要代码: try   {    var bytes: ByteArray = new ByteArray();    bytes.writeMultiByte(DataG ...

  10. super

    [super] Return a proxy object that delegates method calls to a parent or sibling class of type. This ...