http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=638
1 2 3 4 5 0
1 2 5 14 46
第n个人可以从前n-1个人中选出0个人组队,则方法有f[n-1]种;可以从前n-1个人中选出一个人组队,则有n-1种选法,剩下的n-2个人有f[n-2]种组法;可以从前n-1人中选出2两个人组队,则有n-2种选法,剩下的n-3个人有f[n-3[种组法;
递推公式为f[n] = f[n-1] + (n-1)*f[n-2] + (n-1)*(n-2)/2 * f[n-3];
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; typedef long long LL; #define N 1100 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define met(a, b) memset (a, b, sizeof (a)) LL f[N]; void Init () { f[1] = 1; f[2] = 2; f[3] = 5; for (int i=4; i<=20; i++) { LL sum = (i-1)*(i-2)/2; f[i] = f[i-1] + (i-1)*f[i-2] + sum*f[i-3]; } } int main () { int n; Init (); while (scanf ("%d", &n), n) printf ("%I64d\n", f[n]); return 0; }