hdu5524

来源:互联网 发布:雄迈 监控软件密码 编辑:程序博客网 时间:2024/03/29 14:14

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
5678
 

Sample Output
3435
 


参考链接:

官方题解


关于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