(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 |
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
![](/Users/AndyLiu/AppData/Local/YNote/data/qq4BDE4B4C60823CB13C54D710653BEC62/95bb458dd7bf49b1a452a3fec92c3772/clipboard.png)
#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 ...
随机推荐
- 在CentOS上搭建PHP服务器环境
您也可以使用一键自动部署环境的工具,请参见网友开发的这个工具 http://www.centos.bz/2013/08/ezhttp-tutorial/ 安装apache: yum insta ...
- MySQL绿色版5.7以上安装教程
写在前面:5.7增加了安全性,默认root密码不在为空,而是初始化时随机生成一个root密码,改root密码的方式也不一样了 下载地址 http://dev.mysql.com/downloads/m ...
- lua 中的面向对象
lua 是一种脚步语言,语言本身并不具备面向对象的特性. 但是我们依然可以利用语言的特性,模拟出面向对象的特性. 面向对象的特性通常会具备:封装,继承,多态的特性,如何在lua中实现这些特性,最主要的 ...
- 8,SFDC 管理员篇 - 数据模型 - 公式和验证 2
1, Checkbox 只接受真值或者假值 And(arg1, arg2....)至少两个参数,只有参数都为真时候,才返回真,只要有一个为假,就都为假 例如:AND(DoNotCall, HasOpt ...
- linux基础知识(四)
•查看ip地址,ifconfig命令 •重启.启动.停止网络命令 •service network restart/start/stop •VMnet0:用于虚拟桥接网络下的虚拟交换机 •VMne ...
- linux shell学习笔记
一 变量 声明变量: my_var='ddd'使用变量: ${my_var}设置为只读变量: readonly my_var删除变量: unset my_var 注意只读变量不能被删除 变量类型:( ...
- shell-自动更改LINUX服务器IP
#!/bin/bash echo echo == fi i= newgateway= newhostname= cat >>$ipfile<<EOF IPADDR=&q ...
- json 特殊字符 javascript 特殊字符处理(转载)
特殊字符以前都是禁止页面输入,这样就简单不容易出错,但最近需求要求能输入特殊字符整理出java返回json时特殊字符的转义(不转义会破坏json数据格式导致页面读取数据出错) Java代码 publi ...
- poj 2987 最大权闭合图
Language: Default Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8744 Accept ...
- Quartus调用modelsim
1.Quartus 调用modelsim Test Bench Name :是test bench的文件名 Top Level module in test bench:test bench文件内的m ...