http://acm.hdu.edu.cn/showproblem.php?pid=1465
At the beginning of the question, I used the full line to complete the overrun.
1#include"stdio.h"2 intN,count;3 inta[ +];4 intequal ()5 {6 intflag=1;7 for(intI=1; i<=n;i++)8 {9 if(a[i]==i)Tenflag=0; One } A returnFlag; - } - voidPermintk) the { - inttemp; - if(k==N) - { + if(Equal ()) -count++; + } A for(inti=k;i<=n;i++) at { -temp=a[i];a[i]=a[k];a[k]=temp; -Perm (k +1); -temp=a[i];a[i]=a[k];a[k]=temp; - } - } in Main () - { to while(SCANF ("%d", &n)! =EOF) + { - the for(intI=1; i<=n;i++) *a[i]=i; $Perm1);Panax Notoginsengprintf"%d\n", count); -Count=0; the } +}
And then I started thinking about recursive formulas.
Assuming that f (1) and F (2) can be directly mental arithmetic we assume that the former n-1 and n-2 have been lined up.
There are two cases when there are n items
1. Front n-1 Item wrong outfit
2. Front n-2 item wrong outfit
First case
It's easier to n-1*f (n-1) by swapping the nth item with any of the preceding n-1 items
The second case
Well, that's the only thing that's right. Exchange with Nth item but yes, one of the items can be any of the n-2 n-1*f (n-2)
So the recursive formula is f (n) =n-1*f (n-1) +n-1*f (n-2) = (n-1) * (f (n-1) +f (n-2))
This was later found to be the famous wrong-row formula
1#include"stdio.h"2 Main ()3 {4 intN;5 while(SCANF ("%d", &n)! =EOF)6 {7 Long Longa[ -];8a[1]=0; a[2]=1; a[3]=2; 9 for(intI=4; i<=n;i++)Tena[i]= (I-1) * (a[i-2]+a[i-1]); Oneprintf"%i64d\n", A[n]); A } -}
Wrong row problem