uva 991 Safe Salutations

来源:互联网 发布:最长公共子序列c语言 编辑:程序博客网 时间:2024/04/28 21:08

原题:
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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝店链接改了标题没流量怎么办 淘宝手淘搜索自然流量少怎么办 4个月宝贝不食欲下降怎么办 淘宝商家预售时间到了不发货怎么办 淘宝修改类目被处置管控了怎么办 淘宝换手机号是已经注册了的怎么办 闲鱼违规把我淘宝店铺冻结了怎么办 开网店店主想买自己的东西怎么办 以前用微博登录头条的账号怎么办 公众号注册提示邮箱已被占用怎么办 学信网注册时出现邮箱被占用怎么办 手机淘宝店铺接不到买家消息怎么办 买了东西淘宝店铺没了怎么办 淘宝店铺直播间虚假交易违规怎么办 手机淘宝退款物流单号填错了怎么办 淘宝评价删除了卖家没有返现怎么办 想摆摊卖麻辣烫不知怎么摆怎么办 在淘宝卖东西买家退款不退货怎么办 在闲鱼网上买东西被骗了怎么办 开通花呗的手机号不用了怎么办 饿了么开业第一天没单量怎么办 淘宝新开店铺没有人来呀怎么办 刚开的淘宝店铺没有人买东西怎么办 开了一家龙虾店生意很淡怎么办? 新买的皮质小白鞋有点板脚怎么办 手机无效安装包与系统不兼容怎么办 发好的海参放进速冻后变小了怎么办 厨房地砖下面的下水管漏水怎么办? 埋在地下的暖气管漏水怎么办 安装个视频播放器视频全没了怎么办 奇迹mu圣导转职任务打不过怎么办 丈夫死后房产证过给妻子怎么办 学业与志愿活动冲突时你会怎么办 往宅正门前面有半园幕墙玻璃怎么办 模拟城市5石油抽干了怎么办 一户一宅我父亲没有宅基怎么办? 在日本坐新干线期间车票丢了怎么办 街电共享充电宝丢了怎么办 征兵体检时做过疝气手术怎么办 百度网盘手机储存空间不足怎么办 在家带孩子老公不给生活费怎么办