Analysis: Recursion.
When N=1 and 2 o'clock are readily available, it is assumed that F (N-1) and F (N-2) have been obtained, focusing on the following conditions:
When there is N letter, the front N-1 letter can have N-1 or N-2 seal the wrong outfit
The former, for each kind of wrong outfit, can take a letter from N-1 letter and the nth seal wrong outfit, therefore =f (N-1) * (N-1)
The latter is simple, can only be not the wrong one and the nth exchange envelope, the wrong seal can be any of the front N-1 seal, so = F (N-2) * (N-1).
Available, f (n) = (n-1) * (f (n-1) +f (n-2)).
Small amount of data can be directly hit the table.
1#include <cstdio>2 using namespacestd;3 4 intn,sum;5 Long Longres[ +]={0,0,1,2};6 7 intMain ()8 {9 for(intI=4;i< +; i++){Tenres[i]= (I-1) * (res[i-1]+res[i-2]); One } A while(SCANF ("%d", &n)! =EOF) - { -printf"%lld\n", Res[n]); the } - return 0; -}
Recursive representation
1#include <cstdio>2 #defineLL Long Long3 using namespacestd;4 5 intN;6 7LL F (intN)8 {9 if(n==1|| n==0)return 0;Ten if(n==2)return 1; One if(n==3)return 2; A return(n1) * (f (n1) +f (n2)); - } - the intMain () - { - while(~SCANF ("%d",&N)) - { +printf"%lld\n", f (n)); - } + return 0; A}
hdu1465 Not Easy Series