hdu5524

Subtrees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 244    Accepted Submission(s): 132


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. (1N1018)
 

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
 


参考链接:

官方题解


关于long long int 型的左移右移问题,为什么呢?

(今天晚上去吃的酸菜米线,感觉陕西的季节是一个披着冬天的秋天惊讶


已ac的代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;

long long int ans;

long long int dfs(long long int n){
    int deep=log2(n+1);
    long long int temp=1;//因为左移deep后,数是long long int型的,所以1应该是long long int型的,但是直接在1后面加L不行,是为什么呢?

    //printf("%lld %d %lld ",n,deep,(1L)<<deep);
    if((temp<<deep)==n+1){
        if(deep>ans){
            ans=deep;
        }

        //printf("\n");
        return 0;
    }
    else{
        long long int bin=(temp<<(deep-1));
        long long int rem=n-(temp<<deep)+1;

        if(rem>=bin){
            if(deep>ans){
                ans=deep;
            }

            //printf("%d %lld %lld %lld\n",deep,(1L)<<deep,(1L)<<56,n-((1L)<<deep));
            return dfs(n-(temp<<deep))+1;
        }
        else{
            if(deep-1>ans){
                ans=deep-1;
            }

            //printf("\n");
            //printf("%lld %lld\n",(1L)<<(deep-1),n-((1L)<<(deep-1)));
            return dfs(n-(temp<<(deep-1)))+1;
        }
    }
}

int main(){
    long long int n;

    while(scanf("%lld",&n)!=EOF){
        ans=0;
        //printf("wo shi da hao ren");
        printf("%lld\n",ans+dfs(n));
    }

    return 0;
}


看过评论,把temp改成了1LL,ac了,所以表明1是long long 型需要加LL,而不是L,不得不说,当初学的全被就着饭给吃了生气,谢谢评论!!!!

把temp改成了1LL后ac的代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;

long long int ans;

long long int dfs(long long int n){
    int deep=log2(n+1);
    //long long int temp=1;//因为左移deep后,数是long long int型的,所以1应该是long long int型的,但是直接在1后面加L不行,是为什么呢?

    //printf("%lld %d %lld ",n,deep,(1L)<<deep);
    if((1LL<<deep)==n+1){
        if(deep>ans){
            ans=deep;
        }

        //printf("\n");
        return 0;
    }
    else{
        long long int bin=(1LL<<(deep-1));
        long long int rem=n-(1LL<<deep)+1;

        if(rem>=bin){
            if(deep>ans){
                ans=deep;
            }

            //printf("%d %lld %lld %lld\n",deep,(1L)<<deep,(1L)<<56,n-((1L)<<deep));
            return dfs(n-(1LL<<deep))+1;
        }
        else{
            if(deep-1>ans){
                ans=deep-1;
            }

            //printf("\n");
            //printf("%lld %lld\n",(1L)<<(deep-1),n-((1L)<<(deep-1)));
            return dfs(n-(1LL<<(deep-1)))+1;
        }
    }
}

int main(){
    long long int n;

    while(scanf("%lld",&n)!=EOF){
        ans=0;
        //printf("wo shi da hao ren");
        printf("%lld\n",ans+dfs(n));
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值