判断满二叉树中有不同子树的节点有多少个

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\leq N\leq {10}^{18})$

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   题意再说一遍：

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <stack>
using namespace std;
long long maxn;
long long ans;
long long n;
void find(long long x){
long long dep=1;
long long l=x,r=x;
while(l*2<=n){
l=l*2;
dep++;
}
while(r*2+1<=n){
r=r*2+1;
}
if(l<=r){
maxn=max(maxn,dep);
}else{
find(x*2);
find(x*2+1);
ans++;
}
}
int main(){
while(~scanf("%lld",&n)){
maxn=0;
ans=0;
find(1);
cout<<ans+maxn<<endl;
}
return 0;
}

原文作者：满二叉树
原文地址: https://blog.csdn.net/kyoma/article/details/52072829
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。