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=;
scanf("%lld",&T);
while(T--)
{
sum=;
scanf("%lld %lld",&n,&m);
for(int i=n;i>(n-m);i--)
{
sum*=i;
}
for(int i=;i<=m;i++)
{
sum/=i;
}
for(int i=n;i>(n-m);i--)
{
sum*=i;
}
printf("Case %d: %lld\n",t++,sum);
}
return ;
}
Rooks-LightOj1005(规律)的更多相关文章
- 1005 - Rooks(规律)
1005 - Rooks PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...
- Light oj 1005 - Rooks (找规律)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005 纸上画一下,找了一下规律,Ank*Cnk. //#pragma comm ...
- LightOJ1005 Rooks(DP/排列组合)
题目是在n*n的棋盘上放k个车使其不互相攻击的方案数. 首先可以明确的是n*n最多只能合法地放n个车,即每一行都指派一个列去放车. dp[i][j]表示棋盘前i行总共放了j个车的方案数 dp[0][0 ...
- hdu1452 Happy 2004(规律+因子和+积性函数)
Happy 2004 题意:s为2004^x的因子和,求s%29. (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...
- ACM/ICPC 之 DP解有规律的最短路问题(POJ3377)
//POJ3377 //DP解法-解有规律的最短路问题 //Time:1157Ms Memory:12440K #include<iostream> #include<cstring ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- UVA - 11134 Fabled Rooks[贪心 问题分解]
UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...
- 在sqlserver中做fibonacci(斐波那契)规律运算
--利用sqlserver来运算斐波那契规律 --利用事物与存储过程 declare @number intdeclare @A intdeclare @B intdeclare @C int set ...
随机推荐
- ava的动态代理机制详解
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- vuetifyjs简介及其使用
何为 vuetify 一个vue ui库,提供vue组件供使用.根据 Google Material Design 指南实现(https://material.io/).Vuetify支持SSR(服务 ...
- Dragger2解析(一)
依赖注入(DI-Dependency Injection) 什么是依赖注入 这是一种设计思想,一个面向对象的编程法则. DI能够让开发者写出低耦合代码,更加优良的程序. 更容易测试,代码健壮性更强. ...
- Android一句代码给Activity定制标题栏
在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...
- spring jdbc 批处理插入主健重复的数据
1.有事务:当调用spring jdbc 的批处理的时候,在实现层加入事物,只要有插入异常的数据,整个批处理操作都会回滚.事务保证操作的原子性. 2.无事务:当没有事务的时候,批处理插入数据的时候,若 ...
- vue2.0排序应该注意的问题
在computed里新声明了一个对象sortItems,如果不重新声明会污染原来的数据源,这是Vue不允许的,所以你要重新声明一个对象. 如果不重新声明报错: <!DOCTYPE html> ...
- RSA js加密 java解密
1. 首先你要拥有一对公钥.私钥: ``` pubKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1gr+rIfYlaNUNLiFsK/Kn ...
- Harris角点检测原理详解
http://blog.csdn.net/lwzkiller/article/details/54633670 关于角点的应用在图像处理上比较广泛,如图像匹配(FPM特征点匹配).相机标定等.网上也有 ...
- convertquota - 把老的配额文件转换为新的格式
总览 (SYNOPSIS) convertquota [ -ug ] filesystem 描述 (DESCRIPTION) convertquota 把老的配额文件 quota.user 和 quo ...
- react之webpack
1. 下载相关模块包 * 创建package.json ``` npm init ``` * react相关库 package-lock.json ``` npm install react reac ...