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 模型(特别 ...
随机推荐
- type和object
一.定义 1.object是所有新式类的父类 2.type是所有类的类 二.解析 下面通过代码来比较一下object和type的关系(__class__获取所属的类,__bases__获取父 ...
- Linux实现内容分发的主备模式的智能DNS
BIND实现智能DNS的原理是通过view的方式,首先判断客户请求的来源,然后返回不同的IP 规划:为za.com域进行智能解析 分2个网段,192.168.1.0/24网段的请求解析到192.168 ...
- C#语言入门
1.基础知识 2.数据类型 3.控制语句 4.
- Setting title-center on "< h1> " element on Android does not work, fix
app.scss: h1.title-center{ text-align: center!important; }
- BZOJ 5336: [TJOI2018]party
状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...
- (手写)mybatis 核心配置文件和接口不在同一包下的解决方案
smart-sh-mybatis项目 app.xml文件中此处配置为: <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 --> <!-- s ...
- Leetcode 467.环绕字符串中的唯一子字符串
环绕字符串中的唯一子字符串 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"...zabcdef ...
- Spring配置文件中使用ref local与ref bean的区别
Spring配置文件中使用ref local与ref bean的区别.在ApplicationResources.properties文件中,使用<ref bean>与<ref lo ...
- IO Streams:扫描
简介 Scanner类被用于输入的格式化中断,并将其移到Tokens中,然后对其单个的Tokens根据其数据类型进行翻译. 从input--Tokens 默认情况下,一个Scanner使用 空格 键去 ...
- 九度oj 1004
题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17536 解决:4860 题目描述: Given an incre ...