【规律】【贪心】【数学】HDU 5573 Binary Tree
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5573
题目大意:
从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数,最终要求结果为n
输出每层走的数,和是加还是减
题目思路:
【规律】【贪心】【数学】
首先苦思冥想一下,发现,1 2 4 8...2k可以凑成任意的奇数。而偶数只需要把2k变为2k+1。
(从k往1位考虑加减,把+看为1,-看为0,则1+2+4+...=2k-1,符号可以用二进制数X表示,为1111111...,每次X-1,则原式的答案-2)
(如+1+2+4+8=24-1=15,X=1111,则当X=1110时,表示-1+2+4+8=13,X=1101时,表示+1-2+4+8=11,以此类推可以得到任意奇数,偶数同理)
所以可以从2k往前推,当前值比n大就去-,小就取+。
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps 1e-8
#define J 10
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 77
using namespace std;
int cas,cass;
long long n,m,lll,ans;
long long e[N];
void print(int top,int x)
{
if(top==)
{
if(x<n)puts("1 +");
else puts("1 -");
return;
}
if(x>n)
{
print(top-,x-e[top]);
printf("%lld -\n",e[top]);
}
else
{
print(top-,x+e[top]);
printf("%lld +\n",e[top]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
// while(~scanf("%s",s1))
// while(~scanf("%d",&n))
for(e[]=,i=;i<=;i++)
e[i]=e[i-]<<;
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
{
printf("Case #%d:\n",cass);
scanf("%lld%lld",&n,&m);
m--;
if(n&)
{
print(m-,e[m]);
printf("%lld +\n",e[m]);
}
else
{
print(m-,e[m]+);
printf("%lld +\n",e[m]+);
}
}
return ;
} /*
// //
*/
千万不要点
【规律】【贪心】【数学】HDU 5573 Binary Tree的更多相关文章
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- HDU 5573 Binary Tree(找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...
- HDU 5573 Binary Tree(构造题)
http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给出一个满二叉树,根节点权值为1,左儿子为2*val,右儿子为2*val+1.现在有只青蛙从根节点出 ...
- HDU 5573 Binary Tree【构造】
几天前模拟区域赛的一道题,今天发现在草稿箱里直接补个博客. 感觉这还是一道很有意思的构造题. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- hdu 6161--Big binary tree(思维--压缩空间)
题目链接 Problem Description You are given a complete binary tree with n nodes. The root node is numbere ...
随机推荐
- 将JDBC ResultSet结果集变成List
private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>() ...
- rsyslogd配置文件详解
非常详细的rsyslogd配置文件解析 rsyslog服务和logrotate服务=========================================================== ...
- checkbox 删除
先创建del.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- Python开发【第七篇】:面向对象 和 python面向对象(初级篇)(上)
Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...
- OD: Kernel Vulnerabilities
内核漏洞概述 内核漏洞的分类 运行在 Ring0 上的操作系统内核.设备驱动.第三方驱动能共享同一个虚拟地址空间,可以完全访问系统空间的所有内存,而不像用户态进程那样拥有独立私有的内存空间.由于内核程 ...
- NPM环境搭建
1. NPM全局路径:配置npm包的安装位置,在你的用户文件夹下,新建.npmrc文件:cache=D:\NodeJs\nvm\npm-cache 表示缓存文件夹 prefix=D:\NodeJs\n ...
- java常识和好玩的注释
如字符串使用strXXXboolean使用isXXX,hasXXX Vector vProducts= new Vector(); Array aryUsers= new Array(); 类与接口基 ...
- Java简介(4)-关键字
abstract assert boolean break byte byte case catch char class const continue default do-while double ...
- InstallShield Basic MSI工程常见问题解答[转]
1. 问题描述:采用何种安装模式?实现方法:如果对用户界面等自定义要求不高的话,建议用Basic Msi Project,否则用InstallScript MSI Project. 2. 问题描述 ...
- .net别样外观控件包DotNetBar
内容介绍:http://www.componentcn.com/?thread-6423-1.html BubbleBar应用: BubbleBar, DevComponents. Namesp ...