lightoj--1005--Rooks(组合数)

来源:互联网 发布:中文logo设计软件 编辑:程序博客网 时间:2024/05/17 09:04
Rooks
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

SubmitStatus

Description

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 rookR1 and R2 are in attacking positions whereR1 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 putk 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 than1017.

Sample Input

8

1 1

2 1

3 1

4 1

4 2

4 3

4 4

4 5

Sample Output

Case 1: 1

Case 2: 4

Case 3: 9

Case 4: 16

Case 5: 72

Case 6: 96

Case 7: 24

Case 8: 0

Source

Problem Setter: Jane Alam Jan

SubmitStatus


/*以前做组合数总是错,总算找到一种神器#include<stdio.h>#include<string.h>typedef long long LL;LL c[33][33];int main(){int t;scanf("%d",&t);int Case=1;for(int i=1;i<=30;i++){c[i][0]=c[i][i]=1;c[i][1]=i;for(int j=2;j<i;j++){c[i][j]=c[i-1][j]+c[i-1][j-1];}}while(t--){int n,k;LL sum;scanf("%d%d",&n,&k);if(n<k){printf("Case %d: 0\n",Case++);}else {sum=c[n][k]*c[n][k];for(int i=1;i<=k;i++)sum*=i;printf("Case %d: %lld\n",Case++,sum);}}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 备孕喝酒抽烟了怎么办 鼻炎鼻子不通气怎么办速效办法 2岁宝宝智力落后怎么办 六个月宝宝尖足怎么办 3个月宝宝尖足怎么办 2岁宝宝发育慢怎么办 2岁宝宝便秘怎么办呀 2岁宝宝老是便秘怎么办 宝宝4岁不说话怎么办 宜家柜子味道重怎么办 家里有小飞虫怎么办呀 汽车里进老鼠了怎么办 老鼠跑到车里了怎么办 我的小车有老鼠怎么办 车里面进了老鼠怎么办 街电充电宝丢了怎么办 充电宝充不了电怎么办 脚裸扭伤肿了怎么办 大货车电瓶亏电怎么办 货车电瓶被偷了怎么办 小乌龟尾巴断了怎么办 长青春痘怎么办简单小妙招 一关灯就有蚊子怎么办 狗狗误食蟑螂药怎么办 泰迪误食蟑螂药怎么办 猫咪吃了蟑螂药怎么办 狗把蟑螂药吃了怎么办 猫吃了蟑螂诱饵怎么办 猫吃了蟑螂中毒怎么办 孕妇胃烧的难受怎么办 6个月孕妇胃难受怎么办 飞机杯吸盘不上怎么办 我的车位被占用怎么办 占别人车位的车怎么办 头受凉受风了疼怎么办 看电脑时间长了眼睛疼怎么办 电脑玩久了头疼怎么办 屋里有死老鼠味怎么办 老鼠死在车里面怎么办 手机锁屏怎么办求解锁 玩lol左键失灵怎么办