hdu 4372 Count the Buildings
来源:互联网 发布:复杂sql 语句编写案例 编辑:程序博客网 时间:2024/05/17 04:40
题目大意:
给你一个n,表示有n个高度分别为1,2,3……n的楼,然后要求你排列这n个楼的位置,使得从最左端看能看到x个楼,从最右端看到y个楼,问你满足要求的方案数。
数组组数1<=t<=100000,1<=n,x,y<=2000
题解:
解决这题目之前,先给出一个结论:n的环排列的个数与n-1个元素的排列的个数相等。
首先可以肯定,无论从最左边还是从最右边看,最高的那个楼一定是可以看到的,从这里入手。
假设最高的楼的位置固定,最高楼的编号为n,那么我们为了满足条件,可以在楼n的左边分x-1组,右边分y-1组,且用每组最高的那个元素代表这一组,那么楼n的左边,从左到右,组与组之间最高的元素一定是单调递增的,且每组中的最高元素一定排在该组的最左边,每组中的其它元素可以任意排列(相当于这个组中所有元素的环排列)。右边反之亦然。
然后,可以这样考虑这个问题,最高的那个楼左边一定有x-1个组,右边一定有y-1个组,且每组是一个环排列,这就引出了第一类Stirling数(个人分成组,每组内再按特定顺序围圈的分组方法的数目)。我们可以先把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)解决。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;const long long mod=1000000007;long long s[2002][2002],c[2002][2002];int main(){ memset(s,0,sizeof(s)); memset(c,0,sizeof(c)); for(int i=1;i<=2000;i++) { c[i][0]=1; s[i][0]=0; } s[1][1]=1;c[1][1]=1; for(int i=2;i<=2000;i++) for(int j=1;j<=i;j++) { c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod; s[i][j]=(s[i-1][j-1]+(i-1)*s[i-1][j])%mod; } int sec,x,y,n; scanf("%d",&sec); for(int z=1;z<=sec;z++) { scanf("%d%d%d",&n,&x,&y); long long ans; if(x+y-2<=2000)ans=(s[n-1][x+y-2]*c[x+y-2][x-1])%mod;else ans=0%mod;//防止RE printf("%I64d\n",ans); } return 0;}
阅读全文
0 0
- hdu 4372 Count the Buildings
- HDU 4372 Count the Buildings
- hdu 4372 Count the Buildings
- HDU 4372 Count the Buildings
- hdu 4372 Count the Buildings
- HDU 4372 Count the Buildings
- HDU 4372 Count the Buildings
- HDU-4372 Count the Buildings
- hdu 4372 Count the Buildings
- Count the Buildings HDU
- HDU 4372 Count the Buildings 组合数学
- HDU 4372 - Count the Buildings(组合计数)
- [HDU 4372]Count the Buildings(第一类斯特林数+组合数)
- HDU 4372 Count the Buildings(组合数+斯特林数)
- HDU 4372 Count the Buildings(第一类Stirling数)
- 组合数学(斯特林数) hdu 4372 ( Count the Buildings )
- [第一类斯特林数 组合] HDU 4372 Count the Buildings
- hdu 4372 Count the Buildings (Stirling数)
- BSS段、数据段、代码段、堆、栈 简介
- 浅谈用户运营的重要KPI--用户活跃率
- 51单片机串口通信(电脑发送)
- Javascript高级程序设计第七章知识点总结
- CentOS搭建SvnAdmin服务端详细步骤
- hdu 4372 Count the Buildings
- C语言中定义和声明的一些差别
- bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by it
- 【算法思想】对于枚举类型如何来查找相应字符串
- 实验三 顺序队列
- ThreadLocal实现线程范围内的共享变量
- 人性贪婪
- Tensorflow trick 与 细节
- springmvc+mybatis项目报UnsatisfiedDependencyException异常