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 题意再说一遍:
题目将给你一个n,告诉你一棵完全二叉树有多少个节点。在这颗完全二叉树中,每个节点都有不同的size(即其下包涵多少child),要求你判断题中给的完全二叉树中有多少个不同的节点。
对于满二叉树来说,它的答案一定是这个满二叉树的深度。
对于非满完全二叉树来说,每每往下推进一层,题中所求就会增加1
那么求出这颗完全二叉树中的满二叉树最大层数,以及有多少颗非满二叉树
下面是代码:
#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;
}