UVA - 12024 Hats (错排问题)
来源:互联网 发布:网页音频提取软件 编辑:程序博客网 时间:2024/06/05 06:06
Description
Background
John Hatman, the honest cloakroom attendant of the RoyalTheatre of London, would like to know the solution to the followingproblem.
TheProblem
Whenthe show finishes, all spectators in the theatre are in a hurry to see the Final of the UEFAChampionship. So, they run to the cloakroom to take their hats back.
Some of them take a wrong hat. But, how likely is thateveryone take a wrong hat?
TheInput
The first lineof the input contains an integer, t,indicating the number of test cases. For each test case, one lineappears, that contains anumbern, 2<=n<=12,representing the number of people and hats.
TheOutput
For each test case, the output should contain asingle line with the number representing the number of favourable cases(i.e., the number of cases where all people take a wrong hat),followed by a bar, "/", and followed by a number representing thetotal number of possible cases.
SampleInput
3234
SampleOutput
1/22/69/24题意:求每个人拿走不是自己帽子的可能思路:经典的错排问题:当n个编号元素放在n个编号位置,元素编号与位置编号各不对应的方法数用D(n)表示,那么D(n-1)就表示n-1个编号元素放在n-1个编号位置,各不对应的方法数,其它类推.第一步,把第n个元素放在一个位置,比如位置k,一共有n-1种方法;第二步,放编号为k的元素,这时有两种情况:⑴把它放到位置n,那么,对于剩下的n-1个元素,由于第k个元素放到了位置n,剩下n-2个元素就有D(n-2)种方法;⑵第k个元素不把它放到位置n,这时,对于这n-1个元素,有D(n-1)种方法;综上得到D(n) = (n-1) [D(n-2) + D(n-1)]特殊地,D(1) = 0, D(2) = 1.#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 15;ll num1[maxn], num2[maxn];ll n;void init() {num1[1] = 0;num1[2] = 1;num2[1] = 1;num2[2] = 2;for (int i = 3; i < maxn; i++) {num1[i] = (i-1) * (num1[i-2] + num1[i-1]);num2[i] = num2[i-1] * i;}}int main() {init();int t;scanf("%d", &t);while (t--) {scanf("%lld", &n);printf("%lld/%lld\n", num1[n], num2[n]);}return 0;}
- UVA - 12024 Hats (错排问题)
- UVa12024 - Hats(错排问题)
- UVa 10493 Cats, with or without Hats
- Hats’Worlds(字典树)
- [HDU 2068] RPG的错排 (错排问题)
- 错排问题(组合数学)
- 组合数学-错排问题(转)
- 错排问题
- 错排问题
- 错排问题
- 错排问题
- 错排计数问题
- 错排问题
- 错排 问题
- 错排问题:
- 错排问题
- 错排问题
- 错排问题
- MongoVUE破解
- Rsync使用
- pig学习笔记
- 电脑为何无法开机!——————【Badboy】
- 合并excel的各种格式表格
- UVA - 12024 Hats (错排问题)
- 将 storyboard 从 iphone 版转变为 ipad 版
- HDU 1671 简单字典树+内存清理
- iOS CoreBluetooth 教程
- NSURLCache内存缓存
- 使用DrawerLayout实现侧滑菜单
- 磁盘阵列 RAID 技术原理详解 图文
- C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)
- android PullToRefreshListView 4.1上 无效果