hdu 5524 二叉树找规律,二进制相关
input
n 1<=n<=1e18
output
有n个结点的满二叉树有多少个不相同结点数的子树
做法:树有h=log2(n)层,最多有2h-2种(1除外),然后再n减去u重复的即可
#include <bits/stdc++.h>
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <bitset>
#define MAX 100000
#define LL long long
using namespace std;
LL n;
int highbit(LL x)
{
for(int i=;i>=;i--) if(x&(1LL<<i)) return i+;
}
int lowbit(LL x)
{
for(int i=;i<=;i++) if(x&(1LL<<i)) return i;
}
int main()
{
freopen("in","r",stdin);
//scanf("%d",&T);
while(scanf("%lld",&n)==)
{
int h=highbit(n);
LL maxn=(1LL<<h)-;LL mid=maxn-(1LL<<(h->=?h-:));
// printf("%lld:",n);
// printf("h=%d maxn=%lld mid=%lld\n",h,maxn,mid);
if(n==maxn||n==mid) { printf("%d\n",h);continue; }
h+=h-;
if(n<=mid) h--;
h-=lowbit(n-(maxn>>));
printf("%d\n",h);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}
hdu 5524 二叉树找规律,二进制相关的更多相关文章
- HDU 2086 A1 = ? (找规律推导公式 + 水题)(Java版)
Equations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2086 ——每天在线,欢迎留言谈论. 题目大意: 有如下方程:Ai = (Ai-1 ...
- hdu 5047 大数找规律
http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...
- hdu5573 二叉树找规律,二进制相关
input T 1<=T<=100 n k 1<=n<=1e9 n<=2^k<=2^60 output 从1走到第k层,下一层的数是上一层的数×2或者×2+1,每 ...
- hdu 5106 组合数学+找规律
http://acm.hdu.edu.cn/showproblem.php?pid=5106 给定n和r,要求算出[0,r)之间所有n-onebit数的和,n-onebit数是所有数位中1的个数. 对 ...
- hdu 4759 大数+找规律 ***
题目意思很简单. 就是洗牌,抽出奇数和偶数,要么奇数放前面,要么偶数放前面. 总共2^N张牌. 需要问的是,给了A X B Y 问经过若干洗牌后,第A个位置是X,第B个位置是Y 是不是可能的. Ja ...
- Doom HDU - 5239 (找规律+线段树)
题目链接: D - Doom HDU - 5239 题目大意:首先是T组测试样例,然后n个数,m次询问,然后每一次询问给你一个区间,问你这个这段区间的加上上一次的和是多少,查询完之后,这段区间里 ...
- Full Binary Tree(二叉树找规律)
Description In computer science, a binary tree is a tree data structure in which each node has at mo ...
- hdu 4708(暴力+找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 思路:由于N不大,并且我们可以发现通过旋转得到的4个对角线的点的位置关系,以及所要旋转的最小步数 ...
- HDU 5084 HeHe --找规律
题意: 给出矩阵M,求M*M矩阵的r行c列的数,每个查询跟前一个查询的结果有关. 解法: 观察该矩阵得知,令ans = M*M,则 ans[x][y] = (n-1-x行的每个值)*(n-1+y列的每 ...
随机推荐
- cocos2d-x中的init,onEnter,onExit......
CCNode的init()方法和onEnter()方法: 一.调用顺序显示FirstScene时: FirstLayer::initFirstLayer::onEnterFirstLayer::onE ...
- PowerTool(杀毒辅助工具) V4.6 中文免费绿色版
软件名称: PowerTool(杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 968KB图片预览: 软件简 ...
- Php函数完整参考手册
序号 分类 描述 1 Array 函数 2 Calendar 函数 日历扩展包含了简化不同日历格式间的转换的函数. 3 Date/Time 函数 Date/Time 函数用于从 PHP 脚本运行的服务 ...
- 更换jdk版本:jdk1.8更换为jdk1.7之后输入java -version还是出现1.8的版本号
安装了1.7之后修改了JAVA_HOME的环境变量 修改成功之后,在cmd输入java -verson还是出现1.8的版本号 解决办法:将环境变量Path中的%JAVA_HOME%/bin 移到最前面 ...
- ionic的弹出框$ionicPopover
在ionic.html中 在controller.js中
- 小谈数据库Char、VarChar、NVarChar差异
1. char 固定长度,最长n个字符. 2. varchar 最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同) char和varchar区别: ...
- mysql近几天的查询
今天:select*from table1 where to_days(时间字段名)=to_days(now()); 昨天:select*from table1 where to_days(now() ...
- UITableViewCell上面添加UIWebView
首先创建一个webView对象和 private var webViewcell : UIWebView? 在viewDidLoad里面设置创建webViewcell,设置属性并用kvo监听webVi ...
- poj 1308Bugs Integrated, Inc. [三进制状压]
题目链接[http://poj.org/problem?id=1038] 题意: 给出一个N*M大小的图,图中有K个坏点.N (1 <= N <= 150), M (1 <= M & ...
- poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少
import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...