HDU 4372 Count the Buildings

自為風月馬前卒發表於2018-02-07

Count the Buildings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2521    Accepted Submission(s): 817


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
 

 

Source
 

 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4059 2819 1730 2176 1850 
 
 
實在搞不懂HDU的脾氣啊。
為什麼交G++會RE????
這道題的話。
不管是從左邊看還是從右邊看,視線總是會被中間最高的給擋住
所以我們把左邊和右邊分組來看。
對於某一邊,我們確定出能夠看見的樓房,那麼不能夠看見的樓房就可以任意排列
我們把能看見的樓房,與下一個能看到的樓房(不包括下一個樓房)之間的樓看為一組
那麼組內的元素就可以任意排,假設一組有$n$個樓房,那麼它們自由排列的方案數就是$(n-1)!$
這會讓你聯想的到什麼?
哈哈沒錯,第一類斯特靈數(神TM能想到)
這樣我們就可以首先對所有的樓房進行分組,然後再讓他們自由排列
一共有$F-1+B-1$組
 
#include<iostream>
#include<cstdio>
#define LL long long 
using namespace std;
const LL MAXN=2*1e6+10;
LL T,N,F,B;
LL mod=1000000007;
LL c[2011][2011],s[2011][2011];
int main()
{
    for(LL i=0;i<=2010;i++)
    {
        c[i][0]=1;
        c[i][i]=1;
        s[i][0]=0;//無法頻出環 
        s[i][i]=1;//只能拼出一個環 
        for(LL j=1;j<i;j++)
            c[i][j]=(c[i-1][j-1]%mod+c[i-1][j]%mod)%mod,
            s[i][j]=(s[i-1][j-1]%mod+(i-1)%mod*s[i-1][j]%mod)%mod;
    }
    scanf("%I64d",&T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d",&N,&F,&B);
        printf("%I64d\n",(c[F-1+B-1][F-1]%mod*s[N-1][F-1+B-1]%mod)%mod);
    }
    return 0;
}

 

 
 
 
 

相關文章