HDU4372 Buildings
@(HDU)[Stirling數, 排列組合]
Problem Description
There are N buildings standing in a straight line in the City, numbered from 1 to N. The heights of all the buildings are distinct and between 1 and N. You can see F buildings when you standing in front of the first building and looking forward, and B buildings when you are behind the last building and looking backward. A building can be seen if the building is higher than any building between you and it.
Now, given N, F, B, your task is to figure out how many ways all the buildings can be.
Input
First line of the input is a single integer T (T<=100000), indicating there are T test cases followed.
Next T lines, each line consists of three integer N, F, B, (0<N, F, B<=2000) described above.
Output
For each case, you should output the number of ways mod 1000000007(1e9+7).
Sample Input
2
3 2 2
3 2 1
Sample Output
2
1
Solution
好吧, 做這題時我是直接看中文翻譯的 ---- 但是, 當我看到這題原文的時候, 我還是不禁要吐槽出題人的英語水平: 題目描述都是什麼鬼 .. 狗屁不通, 表達的意思完全就不對好嗎 ..
言歸正傳, 先 腦補 翻譯 一下題意:
\(n\)个房子在一条线上(\(n \le 2000\)),高度分别为\(1\)~\(n\),现在需要将房子这样放置:从最左往右能看到\(F\)个房子,从最右往左能看到\(B\)个房子,能看到的条件是 两者之间的房子都要低于这个房子.问这样的方案数.
解法也並不算複雜:
因为肯定能看到最高的,,那我们先假定最高的楼房位置确定,那么在其左边还有\(f-1\)个能看见,在其右边还有\(b-1\)个,能看见 .. 所以可以这样将题目转化: 将除最高楼之外的\(n-1\)个楼,分成\(f-1+b-1\) 组,在最高楼左边\(f-1\) 组,在其右边\(b-1\)组,那么分成\(f-1+b-1\) 组 就是第一类Stirling数.\(s[n-1][f-1+b-1]\) .. 将这\(f-1+b-1\) 任意放在最高的楼房的左边和右边, 顺序是确定的, 两边分别的数量也是确定的, 因此组合数为\(C_{f - 1 + b - 1}^{f - 1}\)
故: 答案為$$ans = s[n - 1][f - 1 + b - 1] * c[f - 1 + b - 1][f - 1]$$
Hint: 輸入的數據可能會不合法, 要加以特判
if(f + b - 2 > n)
puts("0");
代碼:
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
inline int read()
{
int x = 0, flag = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
flag *= - 1;
while(isdigit(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[1 << 5], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int N = 1 << 11;
const int MOD = (int)1e9 + 7;
long long stir[N][N];
int c[N][N];
int main()
{
stir[0][0] = 1;
for(int i = 1; i < N; i ++)
stir[0][i] = (long long)0;
for(long long i = 1; i < N; i ++)
{
stir[i][0] = (long long)0;
for(int j = 1; j <= i; j ++)
stir[i][j] = ((i - 1) * stir[i - 1][j] % MOD + stir[i - 1][j - 1]) % MOD;
}
memset(c, 0, sizeof(c));
c[0][0] = 1;
for(int i = 1; i < N; i ++)
{
c[i][0] = 1;
for(int j = 1; j <= i; j ++)
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
}
int T = read();
while(T --)
{
int n = read(), f = read(), b = read();
if(f + b - 2 >= n)
puts("0");
else
{
int ans = (c[f - 1 + b - 1][f - 1] * stir[n - 1][f - 1 + b - 1]) % MOD;
println(ans);
}
}
}
HDU4372 Buildings的更多相关文章
- [Hdu4372] Count the Buildings
[Hdu4372] Count the Buildings Description There are N buildings standing in a straight line in the C ...
- HDU4372 Count the Buildings —— 组合数 + 第一类斯特林数
题目链接:https://vjudge.net/problem/HDU-4372 Count the Buildings Time Limit: 2000/1000 MS (Java/Others) ...
- HDU4372 Count the Buildings (+题解:斯特林数)
题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...
- [hdu4372]counting buildings
解题关键: n的环排列的个数与n-1个元素的排列的个数相等. 首先可以肯定,无论从最左边还是从最右边看,最高的那个楼一定是可以看到的,从这里入手. 假设最高的楼的位置固定,最高楼的编号为n,那么我们为 ...
- 【HDU4372】Count the Buildings (第一类斯特林数)
Description $N$座高楼,高度均不同且为$1~N$中的数,从前向后看能看到$F$个,从后向前看能看到$B$个,问有多少种可能的排列数. $T$组询问,答案模$1000000007$.其中$ ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- LeetCode Shortest Distance from All Buildings
原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...
- 2015 Multi-University Training Contest 2 1002 Buildings
Buildings Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5301 Mean: n*m列的网格,删除一个格子x,y,用矩形 ...
- LTE Module User Documentation(翻译5)——Mobility Model with Buildings
LTE用户文档 (如有不当的地方,欢迎指正!) 8 Mobility Model with Buildings 我们现在通过例子解释如何在 ns-3 仿真程序中使用 buildings 模型(特别 ...
随机推荐
- 使用Jquery与vuejs操作dom比较
jquery实现添加功能 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 如何用字体在网页中画icon
一.用css雪碧图 1.简介 CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许将一个页面涉及到的所有零星图片都包含到一张大图中, 利用CSS的“background- ...
- 【软件工程】Word frequency program
一.开始写代码前的规划: 1.尝试用C#来写,之前没有学过C#,对于C++也不熟,所以打算先花1天的时间学习C# 2.整个程序基本分为文件遍历.单词提取.单词匹配.排序.输出几个模块,各个模块大致时间 ...
- Ajax、Comet、Websocket、SSE
从 http 协议说起 1996年IETF HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式 ...
- hdu1599 find the mincost route floyd求出最小权值的环
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
问题 E: (ds:图)公路村村通 时间限制: 1 Sec 内存限制: 128 MB 提交: 9 解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...
- (转)Java字符串整形(例:0001)
原文地址:https://blog.csdn.net/xuexiiphone/article/details/51372692 caseID = preFix + String.format(&quo ...
- Java面试题之线程与进程的区别
进程是操作系统分配资源的最小单元: 线程是操作系统调度的最小单元: 一个程序至少有一个进程:一个进程至少有一个线程 每个进程对应一个JVM实例,多个线程共享JVM里的堆: 线程不能看做独立应用,而进程 ...
- The Luckiest number(hdu 2462)
给定一个数,判断是否存在一个全由8组成的数为这个数的倍数 若存在则输出这个数的长度,否则输出0 /* 个人感觉很神的一道题目. 如果有解的话,会有一个p满足:(10^x-1)/9*8=L*p => ...
- 远征(expedition)
[题目描述] 寒枫将军将要带领他的部队去圣雪山消灭那里的冰龙. 部队分成了若干个小队,属于同一个小队的人兵种相同.寒枫将军有着杰出的指挥能力,在战斗的时候,寒枫将军能够让所有相同兵种的人互相配合,使t ...