Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6355 | Accepted: 2922 |
Description
Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this:
- The root contains the pair (1, 1).
- If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b)
Problem
Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right
child?
Input
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*109) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
Output
often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario.
Sample Input
- 3
- 42 1
- 3 4
- 17 73
Sample Output
- Scenario #1:
- 41 0
- Scenario #2:
- 2 1
- Scenario #3:
- 4 6
- 给一个节点,计算从根节点到所给节点的所经过的左节点,和右节点的个数。
#include <cstdio> #include <string.h> #include <cmath> #include <iostream> #include <algorithm> #define WW freopen("output.txt","w",stdout) using namespace std; int main() { int T,w=1; int L,R; int sum; int ans; scanf("%d",&T); while(T--) { scanf("%d %d",&L,&R); sum=0; ans=0; while(L!=1||R!=1) { if(L>R) { int a=(L-1)/R;//进行优化不然会TLE ans+=a; L-=(a*R); } else if(R>L) { int a=(R-1)/L; sum+=a; R-=(L*a); } } printf("Scenario #%d:\n",w++); printf("%d %d\n\n",ans,sum);//两个换行 } return 0;}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏的更多相关文章
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
#include<iostream> #include<stack> #include<stdio.h> using namespace std; struct n ...
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20:56 101人阅读 评论(0) 收藏
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 解题思路: RMQ算法. 不会的可以去看看我总结的RMQ算法. http://blo ...
- Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3706 Accepted: ...
- bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20:15 80人阅读 评论(0) 收藏
这里先只考虑x,y都大于0的情况 如果x^2+y^2=r^2,则(r-x)(r+x)=y*y 令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u&l ...
- Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏
#include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...
随机推荐
- Silverlight TabItem选中,未选中样式设置
<Application.Resources> <LinearGradientBrush x:Key="TabItemBackground" EndPoint=& ...
- MAXFLOAT
CGSizeMake(300, MAXFLOAT),是计算宽和高的,里面的MAXFLOAT通俗点说就是最大的数值,代表你的label的宽和高是随着你label内容而变化,不用担心因为label内容过长 ...
- 利用并查集求最大生成树和最小生成树(nlogn)
hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Eclipse开发C++遇到的Binary not found等问题
但是一些人可能在编写实际程序的时候,主要是执行程序的时候,会遇到如下两条错误信息,而无法进行开发. Launch failed.Bianry not found A program file was ...
- org.openqa.selenium.WebDriverException: f.QueryInterface is not a function Command duration or timeout:
今天偶遇一个问题,运行项目时,发现这个问题: org.openqa.selenium.WebDriverException: f.QueryInterface is not a functionCom ...
- App架构设计学习(一)---- 接口的设计
一.哎,最近换了家工作,结果工作很出的我意外,没有干熟悉的根据需求写代码,反而让我一个小菜鸟去重构一下App的架构(他们公司的app,已经上线了1.0版本了),没办法,只有硬着头皮去先学习学习,再总结 ...
- 夺命雷公狗---DEDECMS----19dedecms栏目列表页的完成
我们的栏目页表页的模版如果不清楚叫什么名,可以通过: 这里很清楚的记录着,我们来看下他: 他长得和我们以前做首页时候是不是很像呢?其实就是一样的,我们改下即可,如下所示: {dede:channel ...
- 夺命雷公狗---DEDECMS----3快速入门之隐藏
如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...
- PHP自动生成后台导航网址的最佳方法
'http://www.jbxue.com'=> '脚本学堂首页', </script>
- 写sql语句分别按日,星期,月,季度,年统计
--写sql语句分别按日,星期,月,季度,年统计销售额 --按日 ' group by day([date]) --按周quarter ' group by datename(week,[date]) ...