您的位置:首页 > 其它

错排数

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的计数情况相同
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: