http://acm.hdu.edu.cn/showproblem.php?pid=4489
The idea of solving the problem is very clear here:
http://blog.csdn.net/bossup/article/details/9915647
Here's how to think about this problem.
Because it is a permutation problem, it is generally starting at a certain point, looking for restrictions, but this problem is different because each data is not the same.
So we need to find the boundary conditions, either from the highest point, or start at the lowest level.
Take the highest point as an example and place the highest point at any point J.
By test instructions can know the arrangement of the two ends of the situation, the front two must be high and low, the back of the two is definitely lower
The total number of permutations at this point is the number of permutations before the highest point, a * the number of permutations after the highest point, b
Highest point front that pile number possible conditions have C (j-1,n-1)
The result is a*b*c (j-1,n-1)
The total number of permutations can be obtained by traversing the possible positions of all the highest points.
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>using namespacestd;#defineMEM (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineDebug printf ("!/m")#defineINF 1100000#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineM 1000000007LL dp[inf][3]; LL cal[ -][ -];//C (m,n)intMain () {LL N,i,j,t,num,ans; //number of record combinations for(i =1; i<= -; i++) {cal[i][0] = Cal[i][i] =1; for(j =1; j<i;j++) { //C (n,m) = C (n-1,m-1) +c (n-1,m)CAL[I][J] = cal[i-1][j-1] + cal[i-1][j]; }} dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] =1; //record total number of methods for(i =2; i<= -; i++)//I is the total number{ans=0; for(j =0; j<i;j++)//J is the highest point position{ans+ = dp[j][0]*dp[i-j-1][1]*cal[i-1][J];//Recursive} dp[i][0]=dp[i][1] = ans/2;//Update DP} SF ("%lld",&N); while(n--) {SF ("%lld%lld",&t,&num); if(num==1) {PF ("%lld 1\n", T); Continue; } PF ("%lld%lld\n", t,dp[num][0]<<1); } return 0;}
HDU 4489 (DP)