USACO 2.3 Cow Pedigrees (DP动态规划)

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputX.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	int totalNodes, theHight;
	scanf("%d%d", &totalNodes, &theHight);

	int trees[100][200];
	int hight, nodes;
	for (hight = 0; hight <= theHight; hight++)
		for (nodes = 0; nodes <= totalNodes; nodes++)
			trees[hight][nodes] = 0;

	for (hight = 1; hight <= theHight; hight++)
		for (nodes = 1; nodes <= totalNodes; nodes += 2){
			if (nodes == 1){
				trees[hight][nodes] = 1;
				continue;
			}
			int theNodes;
			for (theNodes = 1; theNodes <= nodes - 2; theNodes += 2)
				trees[hight][nodes] = (trees[hight][nodes] + trees[hight - 1][theNodes] * trees[hight - 1][nodes - 1 - theNodes]) % 9901;	
		}

	printf("%d\n", (trees[theHight][totalNodes] - trees[theHight - 1][totalNodes] + 9901) % 9901);

#if DEBUG
	}
#endif
	return 0;
}

你可能感兴趣的:(dp,动态规划,USACO,COW,2.3,Pedigrees)