題目大意
輸入 $n(1\le n)$,輸出 $1\sim n$ 錯排的數量。一個排列中所有的元素都不在自己原來的位置就被稱為錯排。
解法
設 $D_n$ 表示 $1\sim n$ 錯排的數量。已知 $D_1=0,D_2=1$ 。
1. 找規律
暴力列出 $n$ 比較小的 case,可以簡單地發現一個規律:
$$D_n=n\times D_{n-1}+(-1)^n$$
Are you sure to leave this team?
Once you delete your team, all team notes will be deleted and cannot be recovered. Please ensure you've exported or transfered these notes.
Enter team name before deleting it:
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up