【USACO2.3.2】奶牛家谱 动态规划

f[k][n]表示 用N个节点,组成深度小于等于K层的方案数。


显然f[k][n] =sum( f[k - 1][p] + f[k - 1][q])  (p + q + 1 = n)


最终答案即为f[k][n] - f[k - 1][n]


PS:short和int运算速度差不多啊卧槽


#include 

unsigned short f[101][201]={0}; //n个节点,组成小于等于k层的方案数
int n, k;
#define mod  9901

int main()
{
	short i, ceng, dian;
	scanf("%d%d", &n, &k);
	for (i = 1; i <= k; ++ i)	f[i][1] = 1; //1个点
	for (ceng = 2; ceng <= k; ++ ceng)
	{
		for (dian = 2; dian <= n; ++ dian)	
		{
			if (ceng <= 10 && dian > (1 << ceng) - 1){f[ceng][dian] = f[ceng - 1][dian]; continue;}
			if (dian < 2 * ceng - 1){f[ceng][dian] = f[ceng - 1][dian]; continue;}
			for (i = 1; i <= dian - 2; ++ i)
				f[ceng][dian] = (f[ceng][dian] + (int)f[ceng - 1][i] * (int)f[ceng - 1][dian - 1 - i]) % mod;
		}
	}
	printf("%d\n", (f[k][n]  + mod - f[k - 1][n]) % mod);
	return 0;
}


你可能感兴趣的:(USACO,DP)