Give an integer N (N<=5000). How many different premutations of sequence 1..n (p[1],p[2]...p[n]) so that p[i]<>i with 1<=i<=n. It means the i-th number in a permutation must be different with its index.
for example
input 2 output 1 (2,1)
input 3 output 2 {(2,3,1); (3,1,2)}
input 4 output 9
Can you give me a solution. thank you very much and sorry about my bad english. :)
from total permutations, subtract number of wrong permutations
Such permutations are called derangements. I believe there is plenty of information about them in the internet.
a[0] = 1
а дальше так: a[n]=(-1)^n + n*a[n-1]
Илиментарная закономерность же, сразу в глаз бросается!!