码迷,mamicode.com
首页 > 其他好文 > 详细

The King’s Ups and Downs

时间:2019-04-06 19:16:12      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:def   col   ==   long   namespace   math.h   bsp   lse   cstring   

有n个高矮不同的士兵,现在要将他们按高,矮依次排列,问有多少种情况。

化简为 n个人,求出可以形成波浪形状的方法数

#include <iostream>
#include <cmath>
#include <math.h>
#include <vector>
#include <cstdio>
#include <cstring>

#include <algorithm>
#define ll long long
using namespace std;
ll dp[25][2];
ll sum[25];
ll C(ll x,ll y)
{
    ll ans = 1;
    if(x < y)
         return 0;
    else if(x == y || y ==0)
         return 1;
    else{
        for(ll i=x;i>=(x-y+1);--i){
            ans *= i;
        }
        while(y){
            ans /= y--;
        }
        return ans;
    }
}
void f()
{
    sum[1] = 1;
    sum[2] = 2;
    dp[0][0] = dp[0][1] = 1;
    dp[1][0] = dp[1][1] = 1;
    dp[2][0] = dp[2][1] = 1;

    for(int i=3;i<=20;i++)
    {
        for(int j=0;j<i;j++)
        {
            sum[i] += dp[j][0]*dp[i-j-1][1]*C(i-1,j);
        }
        dp[i][0] = dp[i][1] = sum[i]/2;
    }
}

int main()
{
    memset(dp,0,sizeof(dp));
    memset(sum,0,sizeof(sum));
    f();
    int n,m,p;
    cin>>n;
    while(n--)
    {
        cin>>m>>p;
        cout<<m<<" "<<sum[p]<<endl;
    }
    return 0;
}

 

The King’s Ups and Downs

标签:def   col   ==   long   namespace   math.h   bsp   lse   cstring   

原文地址:https://www.cnblogs.com/tonyyy/p/10662466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!