Rooks-LightOj1005(规律)

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

自我感觉这题大水,30 15这个数据都过不了   然后就对了

但是我感觉人家写的很好,我当时只想到怎么算出来的   并不知道怎么写的

其实就是规律C(m,n)*A(m,n)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
int main()
{
    long long T,n,m,sum;
    int t=1;
    scanf("%lld",&T);
    while(T--)
    {
        sum=1;
        scanf("%lld %lld",&n,&m);
        for(int i=n;i>(n-m);i--)
        {
            sum*=i;
        }
        for(int i=1;i<=m;i++)
        {
            sum/=i;
        }
        for(int i=n;i>(n-m);i--)
        {
            sum*=i;
        }
        printf("Case %d: %lld\n",t++,sum);
    }
    return 0;
}
时间: 2024-11-25 18:20:17

Rooks-LightOj1005(规律)的相关文章

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

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

LightOJ1005 Rooks(DP/排列组合)

题目是在n*n的棋盘上放k个车使其不互相攻击的方案数. 首先可以明确的是n*n最多只能合法地放n个车,即每一行都指派一个列去放车. dp[i][j]表示棋盘前i行总共放了j个车的方案数 dp[0][0]=1 转移就是从第i-1行转移到第i行,对于第i行要嘛放上一个车要嘛不放,放的话有n-j-1种方法.即dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*(n-j-1). 1 #include<cstdio> 2 #include<cstring> 3 using na

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

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

初二|两级分化时期,你的学习规律养成了吗?

初中数学, 在初中养成良好的学习习惯有助于高中系统性学习,那么你初中的学习规律养成了吗?俗话说,初一不分上下,初二两级分化,初三天上地下,为什么会如此分,我们一起来了解一下. 1 从知识层面讲,初二所学的科目内容多,难度大. 初一我们所学的知识还是蛮多的,而且这些知识都是我们整个初中所学知识的基础部分.基本概念和基本定理比较多,需要记忆的东西也比较多,但是知识的难度都不是很大,而到了初二,数学知识点和难度都有了大幅度的提升,尤其是几何辅助线的大量涉及使很多同学很难适应,摸不着头脑,分式根式的加入

Codeforces 57C Array dp暴力找到规律

主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元就可以. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #

UVA 10479 The Hendrie Sequence 规律

题目大意:一个序列,刚开始由0变到了1,接着往后一个个变化下去 变化的规则是,如果当前数是k,就在这个序列的最后面加上k-1个0,再加上k+1 现在问这个序列的第n个数是多少 解题思路:这是有规律的,第2的k次方个数刚好是k 如果当前数是k,且k刚好是2的n次方,那么这个数前面就有n-1个0,n-2个1,n-3个02组合,以此类推 如果要求第n个数是多少,只需要找到n是哪个k之前的,然后依照上面的规律依次递归下去即可 #include<cstdio> #include<cstring&g

51nod 1161 组合数,规律

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1161 显然,题目可以转化为矩阵求解,但复杂度显然时空都不允许,我们如果自己把这个N*N矩阵的前几项列出来的话就会发现和杨辉三角的某一部分相似, 对照一下发现这个矩阵的第一行对应的就是杨辉三角的某一斜列,依次向下递减,也就是说我们只要知道这几个组合数,就能推导出来这个矩阵. 对于每一个K,对应的矩阵首行元素就是 :  C(k-1,0),C(k,1),C(k+1,2)...