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 模型(特别 ...
随机推荐
- request response cookie session
request 1. url传递参数 1)参数没有命名, 如: users/views def weather(request, city, year): print(city) print(year ...
- python3爬取”理财大视野”中的股票,并分别写入txt、excel和mysql
需求:爬取“理财大视野”网站的排名.代码.名称.市净率.市盈率等信息,并分别写入txt.excel和mysql 环境:python3.6.5 网站:http://www.dashiyetouzi.co ...
- python2与python3的bytes问题
>>> s = '编程' >>> print s 编程 >>> s '\xe7\xbc\x96\xe7\xa8\x8b' >>> ...
- printk的使用技巧
在 linux/kernel.h 中有相应的宏对应. #define KERN_EMERG "<0>" /* system is unusable */#d ...
- Shuffle UVA - 12174 尺取法
题目:题目链接 思路:见紫书,对具体操作方式还不是很理解,代码是从一个题解里看的,以后多回顾下,需要理解 代码: #include <iostream> #include <cstr ...
- UVa - 1593 代码对齐(STL)
看上去十分麻烦的一道题,但是看了看别人的写法感觉大神们写的无比简单. 就是记一个每列单词的最大长度,然后剩下的事交给NB的iomanip头文件就好. stringsteam是一个神奇的东西. #inc ...
- CodeForces 109C 树形DP Lucky Tree
赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...
- VS重置开发环境的方法
经常由于各种插件的原因,导致VS有使用过程中出现断点进不去等各种情况的时候,这个方法可以让你的VS回到最初安装的状态,当然,这时候,各种配置也没有了,不到万不得已就勿使用. 下面以Vs2015来说明: ...
- 光学字符识别OCR-4
经过第一部分,我们已经较好地提取了图像的文本特征,下面进行文字定位. 主要过程分两步: 1.邻近搜索,目的是圈出单行文字: 2.文本切割,目的是将单行文本切割为单字. ...
- 根据已经commit的数据,进行leader和peon之间的同步
Leader Election基本设计 按照rank表示优先级解决冲突问题,为每个monitor预先分配了一个rank 只会接受优先级(rank)比自己高.epoch比上次已接受的epoch大的选举请 ...