题目:http://acm.hdu.edu.cn/showproblem.php?pid=1465
f[n]=(f[n-1]+f[n-2])*(n-1)
有N个信封的时候,可以考虑第N个信封本来是对的。则对于这个信封只有两种选择,一种是找一个错的交换,一种是找另一个对的交换。找一个错的信封交换即从n-1种排错的信封中任取一封交换即f[n-1]*(n-1);另一种找n-1中唯一排对的信封与之交换,即(n-1)*f[n-2]。
由于递推数据很大,要用_int64
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=25;
_int64 n,f[maxn];
int main()
{
f[2]=1;
f[3]=2;
int i;
while(cin>>n)
{
for(i=4;i<=n;i++)
{
f[i]=f[i-2]*f[i-1]*(i-1);
}
cout<<f[n]<<endl;
}
return 0;
}