hdu5524Subtrees+递归模拟

Problem Description
There is a complete binary tree with N nodes.The subtree of the node i has Ai nodes.How many distinct numbers are there of Ai?

Input
There are multiple test cases, no more than 1000 cases.
For each case contains a single integer N on a line.(1≤N≤1018)

Output
The output of each case will be a single integer on a line:the number of subtrees that contain different nodes.

Sample Input

5
6
7
8

Sample Output

3
4
3
5

Source
BestCoder Round #61 (div.2)

问一个N个节点的满二叉树,有多少种不同的节点,该节点的子节点数不同。。
对于一个节点如果它的子树是满二叉树,那么不同的节点数就是满二叉树的层数。如果不是满二叉树,那么这个节点肯定是特别的,
所以类似于那种线段的方法进行编号,逐层递归,如果是满二叉树返回层数,如果不是右左右进行递归。如果继续往下递归,出现了满二叉树,只需要摘一个最大的满二叉树就好了。最后。与根节点合并,
最开始写了一个纯暴力递归。。其实吧。。既然是完全二叉树。满二叉树还是挺多了。 跑的很快。1e18直接秒了。。

#include<bits/stdc++.h>
using namespace std;
#define LL long long


LL fulltree,badtree;
LL n;

void Find(LL x){
    LL l=x,r=x;
    LL deepl=0,deepr=0;;
    while(2*l<=n){
        l=l*2;
        deepl++;
    }
    while(2*r+1<=n){
        r=2*r+1;
        deepr++;
    }
    if(deepl==deepr) fulltree=max(fulltree,deepl);
    else{
        badtree++;
        Find(2*x);
        Find(2*x+1);
    }
}
int main(){

    while(cin>>n){
        fulltree=0;
        badtree=0;
        Find(1);
        cout<<fulltree+badtree+1<<endl;
    }
    return 0;
}


/*暴力,相当于要遍历每个点啊,1e18啊。。
LL ans;
LL n;
set<LL>Q;
set<LL>::iterator it;
LL Find(LL x){
    if(x>n) return 0;
    LL temp=Find(x*2)+Find(x*2+1);
    Q.insert(temp);
    return temp+1;
}
int main(){
    while(cin>>n){
        ans=0;
        Q.clear();
        Find(1);
        //for(it=Q.begin();it!=Q.end();it++) cout<<*it<<endl;
        cout<<Q.size()<<endl;
    }
    return 0;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值