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.(1≤N≤1018)
For each case contains a single integer N on a line.
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
- hdu5524
- hdu5524 Subtrees
- HDU5522【二分】&&HDU5523【。。】&&HDU5524【完全二叉树】
- 【HDU5524 BC61 div1 B】【SET模拟】Subtrees 二叉树不同子节点树的子树个数
- BestCoder Round #61 (div.2)(hdu5522,hdu5523,hdu5524,hdu5525(数论:费马小定理))
- UVa 12112 - Iceman
- The Stanford 3D Scanning Repository
- python PEP8编码规范及问题整理(一)
- 如何统计每个用户发表的各类文章数量
- jquery+ajaxc回调函数内使用$(this)并不能正确找到对象的解决办法
- hdu5524
- OC第六天:Block、数组⾼级
- UIView视图
- 今天上的第一节jquery选择器小结(入门级)
- 狄利克雷传记资料(2011-01-14 22:55:48)
- java线程和小案例
- OC学习 第十一章 协议 内存管理
- The Stanford Bunny
- 安卓 使用socket进行通信