uva 991 Safe Salutations

原题:
As any minimally superstitious person knows all too well, terrible things will happen when four persons
do a crossed handshake.
You, an intrepid computer scientist, are given the task of easing the burden of these people by
providing them with the feasible set of handshakes that include everyone in the group while avoiding
any such crossings. The following figure illustrates the case for 3 pairs of persons:
这里写图片描述
Input
The input to this problem contains several datasets separated by a blank line. Each dataset is simply
an integer n, the number of pairs of people in the group, with 1≤ n ≤ 10.
Output
The output is equally simple. For each dataset, print a single integer indicating the number of possible
crossless handshakes that involve everyone in a group with n pairs of people. Print a blank line between
datasets.
Sample Input
4
Sample Output
14
大意:
给你n对人,这些人围城一个圈。问你在任意两对人握手的方式不能交叉的前提下,n对人有多少种握手方式。

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <queue>
using namespace std;
//fstream input,output;
int f[21],n;

int main()
{
    ios::sync_with_stdio(false);
//  input.open("in.txt");
//  output.open("out.txt");
    memset(f,0,sizeof(f));
    f[2]=f[0]=1;
    for(int i = 2;i <= 10 ; i++ )
    {
        int tmp=0;
        for(int j = 0; j < i ; j++ )
            tmp += f[j*2]*f[i*2-2*j-2];
        f[i*2]=tmp;
    }
    while(cin>>n)
    {
        cout<<f[2*n]<<endl<<endl;
    }
//  input.close();
//  output.close();
    return 0;
}

解答:
刚一看到题目像是卡特蓝数,事实上和卡特蓝数差不多。
首先考虑在圈中任意找两个人握手,即连上一条线。注意,任意找的两个人之间连线以后,这条线的左右两侧剩下的人数必须是个偶数,否则肯定有个人没法握手。
一共有n对人,现在假设已经选定了两个人握手并连上一条线,其这条线的左侧有2*k个人,右侧有2*n-2*k-2个人。则握手的方法有f[2*n-2*k-2]*f[2*k]种方法。现在只要枚举k即可,公式的形式和卡特蓝数雷同。sigma(f[2*n-2*k-2]*f[2*k])就是答案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值