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 ...
随机推荐
- 开篇呀,恭喜恭喜,是个好开头-----关于sort()排序
感觉自己活了半辈子从来没写过博客,这可是头一回,而且不是记事是为了学习,先恭喜恭喜自己,有一个很好的开端,不管能不能半途而废,反正是想着为了学习做点什么. 之前有很多东西需要搬过来,循序渐进吧,反正也 ...
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- Lintcode: Matrix Zigzag Traversal
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-or ...
- vs2015社区版不支持installshield
开始学习vs了 下一步就是在这个平台上提升技术咯. 写了老师上课讲的算法 生成了个VB程序结果不能在别的电脑上运行 .幸好我自己测试了下.要是直接拿去学校不是很~ 然后我就想到当初接触易语言时候的搞静 ...
- 转:Beautiful Soup
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...
- 树形DP+RMQ+单调队列(Bob’s Race HDU4123)
题意:有n个房子,这些房子被n-1条道路连接,有一些运动员从一个房子为起点尽可能跑最远的距离且不能通过一条道路超过两次,这些运行员不能选择同样的起点,这些运动员跑的最远距离和最近距离的差值不能超过Q, ...
- new XMLHttpRequest()和页面关系
1. 三个页面分别对应"自己“的异步对象(3个) <title></title> <script type="text/javascript&quo ...
- cordova3.X 运用grunt生成plugin自定义插件骨架
Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头.麦克风等.Cordova还提供了一组统一的JavaScript类库,以及为这些 ...
- angular 自定义指令
Template-expanding directive: <div ng-controller="Controller"> <div my-customer&g ...
- mongo自带测试
benchrun > res = benchRun({ ops:[{ ns:"test.foo", op:"insert", doc:{y:{,]}} } ...