传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=2068
这道题知道了错排就比较好做了
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
ll C(int m, int n)
{
if (n == 0)
return 1;
ll up = 1;
ll down = 1;
for(int i=m;i>=m-n+1;i--)
up*=i;
for(int i=1;i<=n;i++)
down*=i;
return up/down;
}
int main()
{
int n;
ll inf[30];
inf[0]=inf[1]=0;
inf[2]=1;
for (int i=3;i<=25;i++)
inf[i] = (i - 1) * (inf[i - 1] + inf[i - 2]);
while (cin>>n){
if(n==0)
break;
ll ans = 1;
for (int i=0;i<=n/2;i++)
ans += C(n,i)*inf[i];
cout<