HDU 4372 count the buildings (combined mathematics-Sterling number, combined mathematics-permutation and combination)

Source: Internet
Author: User
Count the buildings
Problem descriptionthere 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.
Inputfirst 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.
Outputfor each case, you should output the number of ways mod 1000000007 (1e9 + 7 ).
Sample Input
23 2 23 2 1
 
Sample output
21
 
Source2012 multi-university training contest 8
Recommendzhuyuanchen520 | we have carefully selected several similar problems for you: 4059 2819 1730 2176 1850

Question:

N heights: 1 ~ N houses are arranged in a row. from the front, we can see f houses. From the back, we can see B houses. How many arrangements do you have?


Solution:

The highest House is in the middle, there is an F-1 house on the left, there is a B-1 house on the right, that is, a total of F + B-2 house, the rest of the House on its left or right, can be understood as divided into the F + B-2 group, and contains the specified order, as the first kind of Sterling number, and then from the F + B-2 Group to select the F-1 group, the answer is: c [F + B-2] [F-1] * s (n-1, F + b-2)

Supplement the sterling number

First Stirling Number S (P, K)

The combination of S (P, K) is the number of methods for arranging P objects into k non-empty loops.
 
Recursive Formula of S (P, K): S (P, K) = (p-1) * s (p-1, k) + S (p-1, k-1 ), 1 <= k <= p-1
Boundary Condition: S (p, 0) = 0, P> = 1 s (P, P) = 1, P> = 0


Description of recursive relationships:
Considering the p-th item, P can constitute a separate non-empty loop arrangement, so that the first p-1st item constitutes a non-empty loop arrangement of the K-1, the number of methods is S (PM, k-1 );
You can also arrange K non-empty items in the P-1 in a circular manner, and insert the P item to the left of the I item, which has (p-1) * s (p-1, K) method.
 
 
Second Class Stirling Number S (P, K)

A combination of S (P, K) is the number of methods used to divide P objects into k non-empty unidentifiable (which can be understood as a box without serial numbers) sets.
K! S (P, K) is the number of methods for dividing P individuals into k different rooms (for example, rooms marked with room numbers) (no room available.

The recursive formula of s (P, K) is: s (P, K) = K * s (p-1) + S (PM, k-1 ), 1 <= k <= p-1
Boundary Condition: S (p, p) = 1, P> = 0 s (P, 0) = 0, P> = 1
  
Description of recursive relationships:
Consider the P item, P can constitute a separate non-empty set, at this time the first p item constitutes a non-empty K-1 of the unidentifiable set, the number of methods is S (PM, k-1 );
You can also set K non-empty and unidentifiable items in P-1, and put the item P in any one. In this way, there are K * s (p-1, k) methods.
  
The first and second types of Sterling numbers have the same initial conditions, but the recurrence relationship is different.

Solution code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;const ll mod=1000000007;const int maxn=2100;ll c[maxn][maxn],s[maxn][maxn];//c[f+b-2][f-1]*s(n-1,f+b-2)void ini(){    for(int i=0;i<maxn;i++){        c[i][0]=c[i][i]=1;        for(int j=1;j<i;j++)            c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;    }    for(int i=0;i<maxn;i++){        s[i][0]=0;        s[i][i]=1;        for(int j=1;j<i;j++){            s[i][j]=(s[i-1][j-1]+(i-1)*s[i-1][j])%mod;        }    }}int n,f,b;int main(){    ini();    int T;    scanf("%d",&T);    while(T-- >0){        scanf("%d%d%d",&n,&f,&b);        ll ans;        if(f+b-2<maxn) ans=( c[f+b-2][f-1]*s[n-1][f+b-2] )%mod;        else ans=0;        cout<<ans<<endl;    }    return 0;}




HDU 4372 count the buildings (combined mathematics-Sterling number, combined mathematics-permutation and combination)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.