标签:des style blog color io os ar strong for
Mean:
略
analyse:
就是错排公式的简单运用。下面来了解一下错排公式。
所谓错排就是全错位排序公式,即被著名数学家欧拉(Leonhard Euler,1707-1783)称为组合数论的一个妙题的“装错信封问题”,他求解这样的问题:
一个人写了n封不同的信及相应的n个不同的信封,他把这n封信都装错了信封,问都装错信封的装法有多少种?
递推公式:f(n)=(n-1) * {f(n-1)+f(n-2)}
Time complexity:O(n)
Source code:
// Memory Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-15-21.27
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;
long long a[N];
void cuopai(long long n) //// Formula : f(n)=(n-1)*{f(n-1)+f(n-2)} ;
{
a[1]=0,a[2]=1;
for(long long i=3;i<=n;i++)
{
a[i]=(i-1)*(a[i-1]+a[i-2]);
}
}
int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
cuopai(30);
int n;
while(cin>>n)
{
cout<<a[n]<<endl;
}
return 0;
}
标签:des style blog color io os ar strong for
原文地址:http://www.cnblogs.com/acmer-jsb/p/3973798.html