HDU 5573 Binary Tree(找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573
题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7。。。)。现在,给你一个N和K,K表示给你这个完全二叉树的前K行,从第1行到第K行有很多路径,希望找到一条路径能表示N,路径上的节点可取正也可取负,要求最后的和为N。
思路:由题目给的数据范围可知前两个节点有一个一定可以表示N。(前两个节点可以表示1 - 2^k)
code:
#include <cstdio>
#include <cmath>
using namespace std;
const int MAXN = ;
typedef long long LL; struct node
{
LL value;
char ch;
};
node rec[MAXN];
void solve(LL p, LL N)
{
int L = ;
while (true) {
if (N < ) {
rec[L].value = p;
rec[L++].ch = '-';
N += p;
p >>= ;
}
else if (N > ) {
rec[L].value = p;
rec[L++].ch = '+';
N -= p;
p >>= ;
}
else return;
}
} int main()
{
int T;
scanf("%d", &T);
for (int cas = ; cas <= T; ++cas) {
LL N;
int K;
scanf("%lld %d", &N, &K);
LL p = (LL)pow(2L, K - ) + ;
if (N & ) --p;
solve(p, N);
printf("Case #%d:\n", cas);
for (int i = K; i >= ; --i) {
printf("%lld %c\n", rec[i].value, rec[i].ch);
}
}
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走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...
- 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 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- HDU 4861 Couple doubi(找规律|费马定理)
Couple doubi Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
随机推荐
- MVC中Json的使用:Controller中Json的处理【转】
一.当查询得到的数据符合前台要求,不需要做任何处理,直接DataList To Json 返回前台. 代码: , out recordCount); return Json(allEntities, ...
- AssetBundle的使用
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Editor ...
- Python核心编程读笔 11:模块
第12章 模块 1.基本概念 模块的文件名就是模块名字.py 每个模块都定义了自己唯一的名称空间 模块的搜索路径:会被保存在 sys 模块的 sys.path 变量里 >>>sys. ...
- 随记1(#define a 10和const int a=10)
正是求职笔试旺季,前几天听说有人遇到此题:#define a 10 和const int a=10的区别,废话不多说,下面来解释一下: #define 指令是定义符号常量 const 定义的是常变 ...
- JVM学习之GC常用算法
出处:博客园左潇龙的技术博客--http://www.cnblogs.com/zuoxiaolong,多谢分享 GC策略解决了哪些问题? 既然是要进行自动GC,那必然会有相应的策略,而这些策略解决了哪 ...
- 动态sql构建的过程
基本原理:使用xsqlbuilder框架完成动态sql的构建. 基本流程:使用WebUtils.getParametersStartingWith(ServletActionContext.getRe ...
- 工作备份 build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion '22.0.0' de ...
- Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql
select msi.segment1 装配件编码 , msi.description 装配件描述 , msi.item_type ...
- 电池和Adapter切换电路改进实验
目的:很多单电池的机器在大负载的情况下,如把背光开到最亮,运行3D游戏,此时拔DC电源很容易出现机器死机,或花屏现象: 原因:Q5的导通时间不够,希望通过G极的快速放电,加快到导通时间: 修改前的电路 ...
- SelectDirectory使用方法以及EnableTaskWindows
SelectDirectory使用方法 格式 Delphi syntax: On Windows: function SelectDirectory(const Caption: string; co ...