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])就是答案。
- UVA 991 Safe Salutations
- UVa:991 Safe Salutations
- UVa 991 - Safe Salutations
- uva 991 Safe Salutations
- UVA 991 - Safe Salutations (卡特兰数)
- UVa 991 Safe Salutations 卡特兰数
- uva 991 - Safe Salutations(卡特兰数)
- Safe Salutations
- UVA991 - Safe Salutations(catalan数)
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- uva 825 - Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- UVA-825-Walking on the Safe Side
- UVa:825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- uva 825 Walking on the Safe Side
- 【POJ】[2236]Wireless Network
- CLLocationManager无法定位问题,CLLocationManagerDelegate不调用didUpdateLocations
- 111. Minimum Depth of Binary Tree
- 【POJ】[1611]The Suspects
- Boost Graph
- uva 991 Safe Salutations
- Python学习之面向对象
- 110. Balanced Binary Tree
- Python元编程-遗忘的远古凶兽
- quick-cocos2dx scheduler.scheduleGlobal坑
- POJ 3074 Sudoku(数独|Dancing Links精确覆盖)
- 深入安卓Package Manager和Package Installer
- Android PackageManagerService分析二:安装APK
- how to calculate the QR decomposition of a matrix