# 【dp专题002】hdu 4489 The King’s Ups and Downs (dp)

Posted by 111qqz on Sunday, November 13, 2016

## TOC

dp[i][0]和dp[i][1]对称，显然相等，都等于sum[i]/2.

``````#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
LL C[30][30];
LL dp[30][2];
LL sum[25];
void cal()
{
for ( int i = 1 ; i <= 20 ; i++)
{
C[i][0] = 1;
C[i][i] = 1;
}
for ( int i = 2; i <= 20 ; i++)
for ( int j = 1 ; j < i ; j++)
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
int main()
{
cal();
/*   for ( int i = 2 ;i  <= 10 ; i++)
{
for ( int j = 0 ; j <= i ; j++)
printf("%d ",C[i][j]);
printf("\n");
} */
ms(sum,0);
sum[1] = 1;
dp[0][0]=dp[0][1]=dp[1][0]=dp[1][1] = 1;
for ( int i = 2 ; i <= 20 ; i++)
{
for ( int j = 0 ; j < i ; j++) //枚举第i个位置前有几个。
{
sum[i] += dp[j][0]*dp[i-1-j][1]*C[i-1][j];
}
dp[i][0] = dp[i][1] = sum[i]/2;
}
//    for ( int i = 1; i <= 20 ; i++) printf("%lld\n",sum[i]);
int T;
cin>>T;
while (T--)
{
int cas,n;
scanf("%d%d",&cas,&n);
printf("%d %lld\n",cas,sum[n]);
}

return 0;
}
``````

「真诚赞赏，手留余香」