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
一.问题分析
首先思考到了每一颗完全二叉树的左子树和右子树必有一颗是满二叉树,满二叉树的种类就是其深度,所以对于树中所有的满二叉树来说,起A的个数一定是最深的满二叉树的深度,那么还有一个比较麻烦的就是非满二叉树的种类,仔细分析一下包含不满的那一部分的二叉树的种类一定和所有的满二叉树的各个部分都是不一样的,那么我们只需再统计有多少个包涵不满的部分的完全二叉树,再加上之前的最深的满二叉树的深度,两者之和就是结果
//
// main.cpp
// HDU 5524
//
// Created by 张嘉韬 on 16/7/29.
// Copyright © 2016年 张嘉韬. All rights reserved.
//
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
long long maxn,ans;
long long n;
long long max(long long a,long long b)
{
if(a>b) return a;
else return b;
}
void find(long long x)
{
long long l,r,dep;
l=x;
r=x;
dep=0;
while(2*l<=n)
{
dep++;
l=2*l;
}
while(2*r+1<=n) r=2*r+1;
if(l<=r)
{
maxn=max(maxn,dep);
}
else
{
find(2*x);
find(2*x+1);
ans++;
}
}
int main(int argc, const char * argv[]) {
//freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
while(scanf("%lld",&n)==1)
{
maxn=0;
ans=0;
find(1);
long long r=ans+maxn+1;
printf("%lld\n",r);
}
return 0;
}