Lightoj 1005 Rooks(DP)

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

题目要求在n*n的棋盘上放k个车,问有多少种方法。

dp[i][j]代表前i行放j个车的方案数。则dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*(n-(j-1));

或者使用组合数学做。答案是C(n,k)*A(n,k)

/* ***********************************************
Author        :guanjun
Created Time  :2016/6/9 16:02:10
File Name     :1005.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
ll dp[33][33];
int n,k;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int T;
    cin>>T;
    for(int t=1;t<=T;t++){
        cin>>n>>k;
        printf("Case %d: ",t);
        if(k>n){
            puts("0");continue;
        }
        cle(dp);
        dp[0][0]=1;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=i;j++){
                if(j)dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*(n-j+1);
                else dp[i][j]=dp[i-1][j];
            }
        }
        printf("%lld\n",dp[n][k]);
    }
    return 0;
}
时间: 2024-10-25 09:34:33

Lightoj 1005 Rooks(DP)的相关文章

LightOJ 1005 Rooks(组合排列)或(dp,还得再看看)

n*n的棋盘中,放k个棋子,每个棋子不能同行同列 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<string> 5 #include<math.h> 6 #include<stack> 7 #include<cstdlib> 8 #include<set> 9 #include<map> 10 #includ

LightOJ - 1005 - Rooks(组合数)

链接: https://vjudge.net/problem/LightOJ-1005 题意: 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 i

1005 - Rooks(规律)

1005 - Rooks   PDF (English) Statistics Forum 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 positio

LightOJ 1364 树形DP

52张扑克牌,问拿到指定数量的4个花色的最少次数期望是多少,其中拿到joker必须马上将其视作一种花色,且要使后续期望最小. 转移很容易想到,主要是两张joker的处理,一个状态除了普通的4个方向的转移,当没拿到joker时还要增加拿到joker的期望,根据题意直接在当前状态下找最小的期望计算即可. /** @Date : 2017-08-29 17:58:59 * @FileName: LightOJ 1364 概率DP.cpp * @Platform: Windows * @Author :

(light OJ 1005) Rooks dp

http://www.lightoj.com/volume_showproblem.php?problem=1005    PDF (English) Statistics Forum 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 vert

Light OJ 1005 - Rooks(DP)

题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这个推导过程如下. 当我们们第n*n的矩阵的时候可以考虑第(n-1)*(n-1)的矩阵经过哪些变换可以变成n*n的. 如上图蓝色方格.我们加入蓝色方格之后,矩阵就会增大一圈. 1.加入我们蓝色方格不放置棋子. dp[n-1][k] 2.加入蓝色方格放置一枚棋子,那么我们其实有三种位置可以放置:(1)右

hdu 5693 &amp;&amp; LightOj 1422 区间DP

hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3组合成. 区间DP,用dp[i][j]表示在i到j之间可以删除的最大数,枚举区间长度,再考虑区间两端是否满足等差数列(这是考虑两个数的),再i到j之间枚举k,分别判断左端点右端点和k是否构成等差数列(还是考虑两个数的),判断左端点,k,右端点是否构成等差数列(这是老驴三个数的) 1 #include

Light oj 1005 - Rooks (找规律)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <c

周赛C题 LightOJ 1047 (DP)

C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They've also decided that no two neighboring houses will be painted