lightoj 1005 Rooks
来源:互联网 发布:知之真切笃实处即是行 编辑:程序博客网 时间:2024/05/18 18:16
A - Rooks
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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 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
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
题目: 国际象棋的棋盘上摆放“车”这个棋子,给你n和k,n是指n * n大小的棋盘,k是指有多少个棋子,求可以摆放的方案总数。
思路:
(1) k>n时 ,方案数为 0 ;
(2) k<=n 时 我们可以看出 第一个人有 n*n 中选择;
第二个人有 (n-1)*(n-1) 种选择;
......
第k个人有(n-k+1)*(n-k+1)中选择;
即 总的方案数 ans = n^2*(n-1)^2 * ...* (n+1-k)^2 ,又由于棋子没差别,所以最终答案 ans = ans / k! ;
由于数据挺大的,就用java写的
import java.util.*;import java.math.*;public class Main {public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T,n,k; T=cin.nextInt(); for(int co=1;co<=T;co++){ n=cin.nextInt(); k=cin.nextInt(); BigInteger a=new BigInteger("1"); if(k>n) System.out.println("Case "+co+": 0"); else{ for(int i=n,j=1;j<=k;j++,i--) { a=a.multiply(BigInteger.valueOf(i)); a=a.multiply(BigInteger.valueOf(i)); a=a.divide(BigInteger.valueOf(j)); } System.out.println("Case "+co+": "+a); } }}}
- LightOJ 1005 - Rooks
- LightOJ 1005 - Rooks
- lightoj 1005 Rooks
- Lightoj 1005 Rooks
- lightoj 1005 - Rooks
- LIGHTOJ 1005 – ROOKS
- LightOJ 1005 Rooks 【排列组合】
- LightOJ 1005 - Rooks (排列组合)
- LightOJ 1005 Rooks
- LightOJ 1005-Rooks
- lightOJ 1005 - Rooks
- lightoj 1005 - Rooks 【组合数学】
- LightOJ 1005 - Rooks(DP)
- lightoj 1005 - Rooks 【组合数学】
- LightOJ - 1005 Rooks(统计)
- LightOJ 1005 - Rooks(计数)
- LightOj 1005 - Rooks 区间dp
- LightOJ 1005 - Rooks 组合数
- Atitit.guice3 ioc 最佳实践 o9o
- vector和map的迭代器失效问题
- 操作系统笔试合集
- NGUI官方实例—ControlIerInput讲解(四)
- Sql Server中三种字符串合并方法的性能比较
- lightoj 1005 Rooks
- image moment图像矩应用:质心与朝向orientation
- 【算法与实现】线段树&树状数组(上)
- NGUI Example5 示例讲解– lights and Refraction
- NGUI官网示例6 – Draggable Window讲解(二)
- hdu 4409 Family Name List(LCA&有坑点)
- NGUI官网示例7-- ScrollView讲解(一)
- COSMIC编译器让代码在RAM中运行的方法
- NGUI官网示例7-- ScrollView讲解(二)