HDU 4372 Count the Buildings (combined mathematics, first Stirling number)
HDU 4372
Question: The height of n buildings is 1 ~ N. from the past, we can see f, and then we can see B, and find out how many sort conditions there may be.
Ideas:
Five hours spent 3.5 hours on the top, and the result was forced to yy out of a recursive formula (later detection .. Yy to 95%, kneeling in all kinds of details, the end of the game did not A drop ..
Only a miracle can be said! However, such a miracle will often be ruined by casual details. orz
After analyzing several groups of data, we can find that the highest building n must be visible, either from left or right.
Then minasan, first fixed the position of building n, the left of building n is divided into F-1 group, B-1 Group on the right, and the highest element in each group represents this group;
Then we can find the left of building n, from left to right,The highest element between groups must be monotonically increasing.!
And the highest elements in each group must be on the leftmost side of the group. Other elements in each group can be arranged in any way (equivalent to the ring arrangement of all elements in this group, the so-called ring arrangement means that an element is fixed at the position of other elements, and the number of solutions is equal to (n-1 )!).
The same applies to the right.
For example, n = 5, B = 3, f = 2.
Obviously, building n must be located at 3rd, or 4th: XX5XX or XXX5X.
When the position is 3, the two elements on the left are divided into two groups, which is obviously in line with the conclusion;
When the position is 4, the three elements on the left are divided into two groups. The possible conditions on the left are: 1 32 5X, 21 3 5X, 2 31 5X, 1 42 5X, 21 4 5X, 2 41 5X, 1 43 5X, 31 4 5X, 3 41 5X...
All meet the conclusion (of course, here X is obviously found XP
Therefore, we can first divide n-1 elements into F-1 + b-1 groups, each group is arranged in the ring, the cumulative number of solutions is the answer.
The number of methods for grouping and arranging loops is called the first stirling number.
Answer: ans (n, f, B) = C [f + B-2] [f-1] * S [n-1] [f + B-2];
Code:
/** @author Novicer* language : C++/C*/#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#define INF 2147483647#define cls(x) memset(x,0,sizeof(x))#define rise(i,a,b) for(int i = a ; i <= b ; i++)using namespace std;const double eps(1e-8);typedef long long lint;const int maxn = 2000 + 5;const lint mod = 1e9 + 7;lint S[maxn][maxn];lint C[maxn][maxn];void init(){ for(int i = 0 ; i < maxn ; i++){ C[i][0]=1; C[i][i]=1; S[i][0]=0; S[i][i]=1; for(int j = 1 ; j < i ; j++){ C[i][j]=(C[i-1][j]%mod+C[i-1][j-1]%mod)%mod; S[i][j]=((i-1)%mod*S[i-1][j]%mod+S[i-1][j-1]%mod); } } } int main(){//freopen(input.txt,r,stdin);int t ; cin >> t;init();while(t--){lint n,f,b;scanf(%I64d%I64d%I64d,&n,&f,&b); lint ans;if(b+f-2 <= 2000) ans=C[f+b-2][f-1]%mod*S[n-1][f+b-2]%mod; elseans = 0 % mod; printf(%I64d,ans);}return 0;}