(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 ...
随机推荐
- sql(转自http://www.imooc.com/article/2325)
http://www.imooc.com/article/2325
- 关于listview的一些属性
由于这两天在做listView的东西,所以整理出来一些我个人认为比较特别的属性,通过设置这样的属性可以做出更加美观的列表 首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你 ...
- Clustering with the ArcGIS API for Flex
Clustering is an excellent technique for visualizing lotss of point data. We've all seen application ...
- php标记,变量,常量
php标记 语法:有4种书写格式 1.<?php ... ?> 强烈推荐使用. 如果当前 php的代码段,是整个文档的最后一段,可以省略结束标记?(建议省略) 每句语句都要以分号;结束. ...
- 后一个div无法遮挡住前一个有img的div
这个标题实在是我无奈之下才取出来的,毕竟我文采有限~ 今天我遇到的一个eggpain的问题: 前提:当页面上有两个没有background的div,大小一样,绝对定位在同一个地方(让第二个遮挡住第一个 ...
- php产生随机数函数
<?php function generate_code($length = 4) { return rand(pow(10,($length-1)), pow(10,$length)-1); ...
- php判断 !==false
测试 if($res !== false){ echo "未定义通过<br>"; }else{ echo "未定义不通过<br>"; } ...
- js中RegExp类型
ECMAScript通过RegExp类型来支持正则表达式. var expression = / pattern / flag ; pattern可以是任意的正则表达式.每个正则都带有标志,用以正则表 ...
- 开源文档管理工具Joomla的网站安装
1.配置PHP开发环境(Apache.PHP.MySQL) 2.安装Joomla网站: 1. 下载安装包 http://www.joomla.org/download.html 2. 登陆Jo ...
- Deep Learning(深度学习)学习笔记整理(二)
本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...