1. 程式人生 > 實用技巧 >[HDU-4372]Count the Buildings

[HDU-4372]Count the Buildings

題目

傳送門

題解

首先,最高的建築是兩邊一定都看得到的,我們先考慮把它放在中間.

剩下的 \(n-1\) 個建築如何放置?由於左邊和右邊是等價的,我們先只考慮左邊的情況.

左邊除開最高的建築,我們還需要看到 \(f-1\) 個建築,注意到這 \(f-1\) 個建築可以不相鄰,我們可以理解為這 \(f-1\) 個可視建築將左邊分成了 \(f-1\) 組,每組裡面,最高的建築放在最左邊,剩下的建築隨便排,這相當於所有建築的圓排列(因為有 \(s[n][1]=(n-1)!\)),而右邊有類似的 \(b-1\) 個組,那麼題目就被轉化為了將 \(n-1\) 個建築分成 \(f+b-2\) 個圓排列的方案數,但是我們要選 \(f-1\)

個放到左邊,那麼還要帶上一個組合數 \({f+b-2}\choose {f-1}\).

但是還有一個問題,對於整體,組與組之間的最大元素遞增,但是我們只是將其選出來了,似乎並沒有滿足題意,但是這個我們可以不用管,因為我們將組選出來之後,排序是他們自己的事,這不干擾選擇.

程式碼

#include<cstdio>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int mod=1000000007;
const int maxn=2000;

int fac[maxn+5],invfac[maxn+5];
int s[maxn+5][maxn+5],C[maxn+5][maxn+5];

inline void Init(){
    s[0][0]=1;
    rep(i,1,maxn){
        s[i][0]=0,s[i][i]=1;
        rep(j,1,i-1){
            s[i][j]=s[i-1][j-1]+1ll*s[i-1][j]*(i-1)%mod;
            if(s[i][j]>=mod)s[i][j]-=mod;
        }
    }
    rep(i,0,maxn){
        C[i][0]=C[i][i]=1;
        rep(j,1,i-1){
            C[i][j]=C[i-1][j-1]+C[i-1][j];
            if(C[i][j]>=mod)C[i][j]-=mod; 
        }
    }
}

int n,f,b;

signed main(){
    Init();
    rep(test_case,1,read(1)){
        n=read(1),f=read(1),b=read(1);
        if(f+b-1>n)writc(0,'\n');
        else writc(1ll*s[n-1][f+b-2]*C[f+b-2][f-1]%mod,'\n');
    }
    return 0;
}