题目链接:

  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的更多相关文章

  1. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  2. HDU 5573 Binary Tree(找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...

  3. HDU 5573 Binary Tree(构造题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给出一个满二叉树,根节点权值为1,左儿子为2*val,右儿子为2*val+1.现在有只青蛙从根节点出 ...

  4. HDU 5573 Binary Tree【构造】

    几天前模拟区域赛的一道题,今天发现在草稿箱里直接补个博客. 感觉这还是一道很有意思的构造题. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 ...

  5. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  8. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  9. hdu 6161--Big binary tree(思维--压缩空间)

    题目链接 Problem Description You are given a complete binary tree with n nodes. The root node is numbere ...

随机推荐

  1. C#自定义事件:属性改变引发事件示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. Getting Started with the NDK

    The Native Development Kit (NDK) is a set of tools that allow you to leverage C and C++ code in your ...

  3. Linux防火墙配置

    防火墙命令 service iptables stop --停止 service iptables start --启动文件  /etc/sysconfig/iptables # Firewall c ...

  4. 动态用javascript来修改单选框性别

    <script> window.onload=function(){ if(<{$data.sex}>==0){//<{$data.sex}>是在数据读出来: do ...

  5. Command 命令模式

    简介 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于[行为请求者]与[行为实现者]解耦,可实现二者之间的松耦合,以便适应变化. 将一个请求封装为一个对象,从而使你可用不同 ...

  6. prim 堆优化+ kruskal 按秩优化

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...

  7. H5移动端的注意细节

    1. max-width用在pc端页面,max-device-width用在移动设备上 2. device-pixel-ratio 设备像素比 3.设备自己单位-物理单位dp/dip css像素 px ...

  8. 跳ajax方式进行前后台交互之后台代码要怎么写

    package com.zq.www.mis.action; import java.util.List; import org.apache.struts2.convention.annotatio ...

  9. memcache的安装及管理

    一.Memcache概述 Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表.(key=value)(是用C语言开发的,并且需要libeven ...

  10. C#基础知识01(continue、break 和 return、ref 和 out)

    break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...