解题关键:

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的更多相关文章

  1. [Hdu4372] Count the Buildings

    [Hdu4372] Count the Buildings Description There are N buildings standing in a straight line in the C ...

  2. HDU4372 Count the Buildings —— 组合数 + 第一类斯特林数

    题目链接:https://vjudge.net/problem/HDU-4372 Count the Buildings Time Limit: 2000/1000 MS (Java/Others)  ...

  3. HDU4372 Buildings

    @(HDU)[Stirling數, 排列組合] Problem Description There are N buildings standing in a straight line in the ...

  4. HDU4372 Count the Buildings (+题解:斯特林数)

    题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...

  5. counting the buildings - 第一类斯特灵数

    2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...

  6. 【HDU4372】Count the Buildings (第一类斯特林数)

    Description $N$座高楼,高度均不同且为$1~N$中的数,从前向后看能看到$F$个,从后向前看能看到$B$个,问有多少种可能的排列数. $T$组询问,答案模$1000000007$.其中$ ...

  7. 杭电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> ...

  8. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  9. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

随机推荐

  1. rails跨域请求配置

    gem 'rack-cors', '~> 0.3.1'application.rb config.middleware.insert_before 0, "Rack::Cors&quo ...

  2. spring-boot2

    1.Spring Boot 1.1.什么是Spring Boot Java是静态语言,先变异后运行都是静态语言,不编译直接运行是动态语言(js是动态语言不需要编译,因为浏览器可以直接解析).Java笨 ...

  3. jQuery横向手风琴

    在线演示 本地下载

  4. Delphi回调函数的使用-例子

    Delphi回调函数的使用-例子 功能大体描述:Form1中有一个Edit和一个Button,当点击BUTTON时弹出FORM2,FORM2中也有一个EDIT和一个BUTTON,当点击FORM2中的B ...

  5. phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建

    phalcon: 按年分表的model怎么建?table2017,table2018...相同名的分表模型怎么建 场景:当前有一张表:Ntime,因为表太大了,考虑要分表: Ntime2017 Nti ...

  6. C#中string.Empty、""和null 之间的区别

    1.C#中string.Empty.""和null 之间的区别 (http://blog.csdn.net/henulwj/article/details/7830615)

  7. gethub的安装

    下载地址: windows的下载地址 https://gitforwindows.org/ 一.安装 win下的安装注意要点: 1.环境变量 2.文件结束符如何处理(windows下/r/n,linu ...

  8. Python习题-登录

    写一个登录的程序,失败次数最多为3次,输入账号.密码错误,提示账号/密码错误.失败三次程序退出,输入正确,提示欢迎xxx登录 i=0while (i<3): username = input(' ...

  9. 【LeetCode】080. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  10. 强烈建议使用国外DNS解析域名,解决访问速度和某些访问故障!

    域名解析的基本原理是把域名翻译成IP地址,以便计算机能够进一步通信,传递网址和内容等. 域名劫持就是在劫持的网络范围内拦截域名解析的请求,分析请求的域名,把审查范围以外的请求放行,否则直接返回假的IP ...