[usaco] Cow Pedigrees

简介: <p>Cow Pedigrees<br> Silviu Ganceanu -- 2003</p> <p>Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships amo

Cow Pedigrees
Silviu Ganceanu -- 2003

Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships among the cows can easily be represented by one or more binary trees with a total of N (3 <= N < 200) nodes. The trees have these properties:

The degree of each node is 0 or 2. The degree is the count of the node's immediate children.
The height of the tree is equal to K (1 < K <100). The height is the number of nodes on the longest path from the root to any leaf; a leaf is a node with no children.
How many different possible pedigree structures are there? A pedigree is different if its tree structure differs from that of another pedigree. Output the remainder when the total number of different possible pedigrees is divided by 9901.

PROGRAM NAME: nocows
INPUT FORMAT
Line 1: Two space-separated integers, N and K.
SAMPLE INPUT (file nocows.in)
5 3

OUTPUT FORMAT
Line 1: One single integer number representing the number of possible pedigrees MODULO 9901.
SAMPLE OUTPUT (file nocows.out)
2

OUTPUT DETAILS
Two possible pedigrees have 5 nodes and height equal to 3:
           @                   @     
          / \                 / \
         @   @      and      @   @
        / \                     / \
       @   @                   @   @

--------------------------------------------------------------------------------------------------------
这是个典型的动态规划的问题:
欲求一个高度为K的二叉树,要从一个等于K-1,一个小于等于K-1的两个二叉树构造而来。
关键之处在于,用哈希映射来快速的查找。
还有,是自底向上构造二叉树,还是自上向下的构造二叉树。根据动态规划,类似于斐波纳旗公式,存在很多重复计算的问题,
因此自底向上计算的,可以减少重复。

还有减枝的问题。对于高度为K的二叉树,节点数是2×K-1 -》2^k-1;对于超过N的节点,就不必计算了。
这会减少很多计算。
还有,节点数不再这个范围内的数,直接返回0就不必计算。

我的解法:
--------------------------------------------------------------------------------------------------------


/*
ID: yunleis2
PROG: nocows
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

int ind[203]; 
 
class unit
{
public:
	int c;
	int h;
	int n;
	unit(int c,int h,int n)
	{
		this->c=c;
		this->h=h;
		this->n=n;
	}
	unit(){}
	friend ostream & operator <<(ostream & out,unit u)
	{
		out<<u.c<<"\t"<<u.h<<"\t"<<u.n<<endl;
		return out;
	}
};
vector<unit> v; 
#define max(x,y)  (x>y?x:y)

int main()
{
	fstream fin("nocows.in",ios::in);
	int N,K;
	fin>>N>>K;
	unit u(1,1,1);
	v.push_back(u);

	for(int i=0;i<203;i++)
		ind[i]=-1;
	int total=0;
	int ptr=0;
	vector<unit> v1;
	
	 

	bool flag=false;
	if((N<(2*K-1))||(N>(pow((double)2,K)-1)))
		flag=true;
	while(!flag)
	{ 

		int i;
		for(i=ptr;i<v.size()&&!flag;i++)
		{
			
			unit u1=v.at(i);
			 
			if((u1.h)>=K)
			{
				flag=true;
				break;
			}
			for(int j=0;j<=i;j++)
			{
				unit u2=v.at(j);
				
				int c=1+u1.c+u2.c;
				if(c>N)
					continue;
				int h=max(u1.h,u2.h)+1;
				int n=u1.n*u2.n%9901;
				if(i!=j)
					n=2*n;
				unit u(c,h,n);
				//v.push_back(u);
				//add to the temp vec
				if(ind[u.c]==-1){
					v1.push_back(u);
					ind[u.c]=v1.size()-1;//ind[u.c]=v1.size()-1;
				}
				else
				{
					v1.at(ind[u.c]).n+=u.n;
				}
			}
		}
		for(int s=0;s<v1.size();s++)
		{
			v1.at(s).n=v1.at(s).n%9901;
			v.push_back(v1.at(s));
			ind[v1.at(s).c]=-1;
			cout<<v1.at(s);
			if(v1.at(s).c==N&&v1.at(s).h==K)
			{
				flag=true;
				total=v1.at(s).n;
				break;
			}
			
		}
		
		v1.clear(); 
		ptr=i;
		
	}
	
	
	 
	 
	fstream fout("nocows.out",ios::out);
	fout<<total<<endl;
	 
	 
  return 0;

 }



 

目录
相关文章
|
8天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
6天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
334 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
19天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1331 8
|
7天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
322 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
18天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1416 87
|
5天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
7天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
257 82
2025年阿里云域名备案流程(新手图文详细流程)