(light OJ 1005) Rooks dp
| 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 |







#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的更多相关文章
- (期望)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 ...
- (状压) Brush (IV) (Light OJ 1018)
http://www.lightoj.com/volume_showproblem.php?problem=1018 Mubashwir returned home from the contes ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- (light oj 1319) Monkey Tradition 中国剩余定理(CRT)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...
- (light oj 1024) Eid (最小公倍数)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1024 In a strange planet there are n races. ...
- Light OJ 1005 - Rooks(DP)
题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...
- Light oj 1005 - Rooks (找规律)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. //#pragma comm ...
- Light OJ 1005 - Rooks 数学题解
版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 1002 - Country Roads(light oj)
1002 - Country Roads I am going to my home. There are many cities and many bi-directional roads betw ...
随机推荐
- Android dispatchTouchEvent介绍
一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_ ...
- Python学习——基础篇
1.python的安装 python下载地址:https://www.python.org/downloads/ 安装完成后,运行cmd.exe,输入python 如果出现“p ...
- Oracle 表分区
从以下几个方面来整理关于分区表的概念及操作: 表空间及分区表的概念 表分区的具体作用 表分区的优缺点 表分区的几种类型及操作方法 对表分区的维护性操作 1.表空间及分区表的概念 表空间: 是一个或多个 ...
- CSS从大图片上截取小图标的操作
注:图片名称(tabicons.png)每个小图标width:18px;height:18px从左上角坐标为(-0px;-0px;); 例如第一个对号的坐标为(-0px;-0px;)第二个加号的图标为 ...
- phpexcel简单用法
<?php /*php生成excel完整实例代码现求:php生成excel完整实例代码最好能说明如何调用!谢谢java_sunhui4 | 浏览 8131 次 2014-09-24 14:502 ...
- 使用get传参的时候,参数在后头获取不到或者出现别的错误。
把传递的参数使用encode转换一下,符合HTTP规定的编码,再使用. String encode = java.net.URLEncoder.encode("VSrYJoDat8z7Ad9 ...
- Egret版本更新(H5增加版本号)
由于浏览器缓存问题.在服务器上更新了新的图片等资源以后,客户端并不会下载最新的. 关于浏览器缓存机制,可自行百度. Egret中资源更新解决方案有以下: 一 资源名后增加版本号 二 重写Egret引擎 ...
- VS2012调试时无法启动程序和拒绝访问问题汇总
很多人在使用VS2012的时候会出现下面所示的问题,我也是,而且不止一次,也不是同样的问题,我这里就把一些常见的解决方法罗列一下.
- C++基于TCP和UDP的socket通信
以下是关于socket编程的一个非常经典的例子: 服务端: #include <stdio.h> #include <Winsock2.h> //windows socket的 ...
- VC++ 关于 ON_UPDATE_COMMAND_UI 相关的作用.
ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort) ON_UPDATE_COMMAND_UI_RANGE ...
