POJ 1671

其实求的是BELL数,即前N个第二类斯特林数的和。

一首诗有n行,每一行有一种韵律,问这首诗总共可能有多少种韵律排列。如4行,则所有的15种情况为:aaaa, aaab, aaba, aabb, aabc, abaa, abab, abac, abba, abbb, abbc, abca, a bcb, abcc, and abcd

即每首诗可能只有一种,两种。。。N种韵律。对应第二类斯特林数,即是把N行诗放入K种韵律的盒子中。求和。

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
double str[55][55];

void initial(){
	for(int i=0;i<55;i++){
		for(int j=0;j<=i;j++){
			if(i==j) str[i][j]=1;
			else if(j==0&&i>=1) str[i][j]=0;
			else{
				str[i][j]=j*str[i-1][j]+str[i-1][j-1];
			}
		}
	}
}


int main(){
	initial();
	int n;
	while(scanf("%d",&n),n){
		double ans=0;
		for(int i=0;i<=n;i++)
		ans+=(str[n][i]);
		printf("%d %0.lf
",n,ans);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4020343.html