HDU 5524 Subtrees
来源:互联网 发布:linux 通配符 编辑:程序博客网 时间:2024/06/02 21:24
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
先找出能形成的最大的个数为2^k-1的子树,里面有k-1种,然后对于n所在的那一条链往上找,只要不是满的情况就是一种,然后累加上去就好了
#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 105;long long T, n, s, t, ans, a[maxn];int main(){ //scanf("%d", &T); for (int i = a[0] = 1; i < maxn; i++) a[i] = a[i - 1] << 1; for (int i = a[0] = 1; i < maxn; i++) a[i] = a[i] - 1; while (scanf("%I64d", &n) != EOF) { for (int i = 1; i < maxn; i++) { if (n == a[i]) { ans = i; break; } if (a[i] < n && n < a[i + 1]) { long long k = n - a[i]; if (k + k < a[i] + 1) ans = i - 1; else ans = i; int flag = (n & 1) ^ 1; for (long long i = n >> 1; i; i >>= 1) { ans += flag; if (flag == 0 && i % 2 == 0) flag = 1; } break; } } printf("%I64d\n", ans); } return 0;}
0 0
- HDU 5524 Subtrees
- HDU 5524 Subtrees(DFS)
- HDU 5524 Subtrees
- HDU 5524:Subtrees
- hdu 5524 Subtrees(高效)
- 练习:hdu 5523 Game + hdu 5524 Subtrees
- HDU 5524 Subtrees [想法题/LCA]
- HDU 5524 Subtrees(完全二叉树)
- HDOJ 5524 Subtrees
- HDOJ 5524 Subtrees(二叉树)
- hdu5524 Subtrees
- Hdu 4013 Distinct Subtrees (状态压缩枚举+树的最小表示)
- Leetcode: Count Univalue Subtrees
- *LeetCode-Count Univalue Subtrees
- BC 61 Subtrees
- [LeetCode250]Count Univalue Subtrees
- Count Univalue Subtrees
- Count Univalue Subtrees
- Android任务管理器的设计实现
- HDU 5523 Game
- Leap Motion 使用OpenCV获取和显示图像
- HDU5525(乘法原理)
- SDUT 2500 统计N!中包含末尾0 的个数
- HDU 5524 Subtrees
- python 文件读写查找、替换相关简单操作
- BestCoder Round #61 (div.2) HDU5523 Game
- git 服务器搭建 for linux
- HDU-5512 Pagodas(GCD)
- 数据结构实验一(1)
- Http Chunked Transfer Coding
- javaScript对象详解
- Java生产者消费者例子尝试