[hdu4372]counting buildings
解题关键:
n的环排列的个数与n-1个元素的排列的个数相等。
首先可以肯定,无论从最左边还是从最右边看,最高的那个楼一定是可以看到的,从这里入手。
假设最高的楼的位置固定,最高楼的编号为n,那么我们为了满足条件,可以在楼n的左边分x-1组,右边分y-1组,且用每组最高的那个元素代表这一组,那么楼n的左边,从左到右,组与组之间最高的元素一定是单调递增的,且每组中的最高元素一定排在该组的最左边,每组中的其它元素可以任意排列(相当于这个组中所有元素的环排列)。右边反之亦然。
最高的那个楼左边一定有x-1个组,右边一定有y-1个组,且每组是一个环排列,这就引出了第一类Stirling数($n$个人分成$k$组,每组内再按特定顺序围圈的分组方法的数目)。我们可以先把n-1个元素分成x-1+y-1组,然后每组内部做环排列。再在所有组中选取x-1组放到楼n的左边。所以答案是Stirling[n-1][x-1+y-1]*C[x-1+y-1][x-1](组合数);
预处理$O(N^2)$。对于每组询问$O(1)$解决。
第一类stirling数是环之间是无顺序的
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=;
const int mod=1e9+;
ll comb1[maxn][maxn],stir[maxn][maxn];//2w*2w会爆掉
void init1(){
comb1[][]=stir[][]=;
for(int i=;i<maxn;i++){
comb1[i][i]=comb1[i][]=;
stir[i][]=;stir[i][i]=;
for(int j=;j<i;j++){
comb1[i][j]=(comb1[i-][j-]+comb1[i-][j])%mod;
stir[i][j]=((i-)*stir[i-][j]+stir[i-][j-])%mod;
}
}
} int main(){
int t,f,b,n;
init1();
cin>>t;
while(t--){
cin>>n>>f>>b;
ll ans=;
if(f+b-<=) ans=(1ll*stir[n-][b+f-]*comb1[b+f-][b-]+mod)%mod;
else ans=;
cout<<ans<<"\n";
}
return ;
}
[hdu4372]counting 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 Buildings
@(HDU)[Stirling數, 排列組合] Problem Description There are N buildings standing in a straight line in the ...
- HDU4372 Count the Buildings (+题解:斯特林数)
题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...
- counting the buildings - 第一类斯特灵数
2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...
- 【HDU4372】Count the Buildings (第一类斯特林数)
Description $N$座高楼,高度均不同且为$1~N$中的数,从前向后看能看到$F$个,从后向前看能看到$B$个,问有多少种可能的排列数. $T$组询问,答案模$1000000007$.其中$ ...
- 杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》
http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
随机推荐
- [原创]java WEB学习笔记28: 会话与状态管理Cookie 机制
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- server.xml笔记
本文总结自: http://www.importnew.com/26156.html 核心元素: 顶层元素: server service 连接器: connector 容器: engine > ...
- SQl Server 中登录名 、用户、角色、概念一览
转载:http://www.2cto.com/database/201306/216922.html 数据库,角色,用户,安全 登录SQL server 2008可以用w ...
- 算法(Algorithms)第4版 练习 2.1.4
E A S Y Q U E S T I O N A E S Y Q U E S T I O N A E S Y Q U E S T I O N A E S Y Q U E S T I O N A E ...
- php 二维数组验证一个值是否存在
php 判断数字在二维数组里 $arr = array( array('a', 'b'), array('c', 'd') ); in_array('a', $arr); // 此时返回的永远都是 f ...
- 2018.5.8 Project review
1 .product introduced A. Function requirement (customer) The product function is control the 1KW and ...
- nodejs 解析 base64 文本
使用Buffer对象,在创建Buffer,指定源的编码方式 Buffer.from(data, 'base64').toString(); 当然如果是解析图片或者二进制数据的话,就不需要toStrin ...
- Tomcat报错:HTTP Status 500 - Wrapper cannot find servlet class
HTTP Status 500 - Wrapper cannot find servlet class com.servlet.servlet.RegServlet or a class it dep ...
- Mybatis学习--动态SQL
学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html MyBatis 的强大特性之一便是它的动态 SQL. ...
- 分享几个高效编写JS 的心得
原则 不要做任何优化除非的确需要优化 任何的性能优化都必须以测量数据为基础,如果你怀疑代码存在性能问题,首先通过测试来验证你的想法.性能优化三问 我还能做哪些工作从而让代码变得更有效率? 流行的J ...