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:
uva 991 Safe Salutations_第1张图片
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 
#include 
#include 
#include 
#include 
#include 
#include 
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<2*n]<//  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])就是答案。

你可能感兴趣的:(数学,uva)