HDU 1292 "下沙野骆驼"ACM夏令营(DP)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1292

思路:dp[i][j]表示前j个人分成i队,dp[i][j] = dp[i-1][j-1] + dp[i][j-1] * i表示第j个人单独成一队,或者在前i队中选择一个插入

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
LL dp[30][30];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
            dp[1][i] = dp[i][i] = 1;
        LL sum = 1;//加上dp[1][n]的值
        for(int i=2; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                dp[i][j] = dp[i-1][j-1] + dp[i][j-1] * i;
            }
            sum += dp[i][n];
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


你可能感兴趣的:(HDU 1292 "下沙野骆驼"ACM夏令营(DP))