版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

仓库源文站点原文


title: "题解 - [UVa 10497] Sweet Child Makes Trouble" categories:


题目链接

<!-- more -->

原始题面

Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see how Tintin, a five year’s old boy, creates trouble for his parents. Tintin is a joyful boy and is always busy in doing something. But what he does is not always pleasant for his parents. He likes most to play with household things like his father’s wristwatch or his mother’s comb. After his playing he places it in some other place. Tintin is very intelligent and a boy with a very sharp memory. To make things worse for his parents, he never returns the things he has taken for playing to their original places

Think about a morning when Tintin has managed to ‘steal’ three household objects. Now, in how many ways he can place those things such that nothing is placed in their original place. Tintin does not like to give his parents that much trouble. So, he does not leave anything in a completely new place; he merely permutes the objects

Input

There will be several test cases. Each will have a positive integer less than or equal to 800 indicating the number of things Tintin has taken for playing. Each integer will be in a line by itself. The input is terminated by a ‘-1’ (minus one) in a single line, which should not be processed

Output

For each test case print an integer indicating in how many ways Tintin can rearrange the things he has taken

Sample Input

2
3
4
-1

Sample Output

1
2
9

题意简述

求 $n$ 个元素的错排数

解题思路

板子题, 唯一要注意的就是要用高精

设 $D(n)$ 为 $n$ 个元素的错排数, 则

$$ D(n)=\begin{cases} 0,&n=1\ 1,&n=2\ (n-1)(D(n-1)+D(n-2)),&n>2 \end{cases} $$

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb cpa_cpp title:UVA_10497 UVA/10497/0.cpp %} </details>