HDU 4489 The King's Ups and Downs

HDU 4489 The King's Ups and Downs

思路:

状态:dp[i]表示i个数的方案数。

转移方程:dp[n]=∑dp[j-1]/2*dp[n-j]/2*C(n-1,j-1)。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=55;
ll dp[N];
ll C(ll n,ll m)
{
    ll t1=1,t2=1;
    if(n-m<m)m=n-m;
    for(int i=0;i<m;i++)
    {
        t1*=(n-i);
        t2*=(i+1);
    }
    return t1/t2;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,a,b;
    dp[0]=dp[1]=2;
    for(int i=2;i<=20;i++)
    {
        ll tot=0;
        for(int j=1;j<=i;j++)
        {
            tot+=dp[j-1]/2*dp[i-j]/2*C(i-1,j-1);
        }
        dp[i]=tot;
    }
    dp[1]=1;
    cin>>n;
    while(n--)
    {
        cin>>a>>b;
        cout<<a<<' '<<dp[b]<<endl;
    } 
    return 0;
} 

 

posted @ 2017-10-25 20:01  Wisdom+.+  阅读(210)  评论(0编辑  收藏  举报