错排数
2015-11-23 19:53
232 查看
Count Derangements (Permutation such that no element appears in its original position)
A Derangement is a permutation of n elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0}.
Given a number n, find total number of Derangements of a set of n elements.
Examples:
Input: n = 2
Output: 1
For two elements say {0, 1}, there is only one
possible derangement {1, 0}
Input: n = 3
Output: 2
For three elements say {0, 1, 2}, there are two
possible derangements {2, 0, 1} and {1, 2, 0}
Input: n = 4
Output: 9
For four elements say {0, 1, 2, 3}, there are 9
possible derangements {1, 0, 3, 2} {1, 2, 3, 0}
{1, 3, 0, 2}, {2, 3, 0, 1}, {2, 0, 3, 1}, {2, 3,
1, 0}, {3, 0, 1, 2}, {3, 2, 0, 1} and {3, 2, 1, 0}
We strongly recommend you to minimize your browser and try this yourself first.
Let countDer(n) be count of derangements for n elements. Below is recursive relation for it.
下面是递推公式
countDer(n) = (n-1)*[countDer(n-1) + countDer(n-2)]
好难理解,先记下。链接是http://www.geeksforgeeks.org/count-derangements-permutation-such-that-no-element-appears-in-its-original-position/
对countDer(n-1)的暂时理解是类比,类比如果是n个人的的话,那么跟不选0的计数情况相同
A Derangement is a permutation of n elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0}.
Given a number n, find total number of Derangements of a set of n elements.
Examples:
Input: n = 2
Output: 1
For two elements say {0, 1}, there is only one
possible derangement {1, 0}
Input: n = 3
Output: 2
For three elements say {0, 1, 2}, there are two
possible derangements {2, 0, 1} and {1, 2, 0}
Input: n = 4
Output: 9
For four elements say {0, 1, 2, 3}, there are 9
possible derangements {1, 0, 3, 2} {1, 2, 3, 0}
{1, 3, 0, 2}, {2, 3, 0, 1}, {2, 0, 3, 1}, {2, 3,
1, 0}, {3, 0, 1, 2}, {3, 2, 0, 1} and {3, 2, 1, 0}
We strongly recommend you to minimize your browser and try this yourself first.
Let countDer(n) be count of derangements for n elements. Below is recursive relation for it.
下面是递推公式
countDer(n) = (n-1)*[countDer(n-1) + countDer(n-2)]
好难理解,先记下。链接是http://www.geeksforgeeks.org/count-derangements-permutation-such-that-no-element-appears-in-its-original-position/
对countDer(n-1)的暂时理解是类比,类比如果是n个人的的话,那么跟不选0的计数情况相同
相关文章推荐
- 深入理解JDBC的超时设置
- LoadRunner中负载时间Duration与迭代次数Iteration的联系
- SharedSdk 分享
- mysql问题之infomation_schema
- [转]Bluetooth LE Credit-Based Flow Control for L2CAP Connection-Oriented Channels
- Project Euler 82:Path sum: three ways 路径和:3个方向
- 如何使用reg命令修改注册表
- DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较
- ajax-瀑布流效果
- Golang学习笔记:包制作
- codeforces 581A Vasya the Hipster
- 数据结构之队列_Queue
- iOS求职:OC面试题
- 《奔跑吧,兄弟》之王祖蓝的"钥匙配箱子"概率统计问题--->>回眸
- [android] cannot be cast to android.widget.HeaderViewListAdapter
- CityEngine CGA语法之单坡式屋顶函数 roofShed
- codeforces 581B Luxurious Houses
- 阿里巴巴集团去IOE运动的思考与总结
- 设计模式 - 创建型模式 - 工厂模式
- js将时间戳转换成日期格式