Background 
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

The first line contains the number of scenarios. 
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*10 9) that represent 
a node (i, j). You can assume that this is a valid node in the binary tree described above.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how 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<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define inf 0x3f3f3f3f
using namespace std; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n,l,r;
while(cin>>n)
{
int tt=;
while(n--)
{
cin>>l>>r;
if(l==)
{
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<<<" "<<r-<<endl;
if(n)
cout<<endl;
continue;
}
else if(r==)
{
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<l-<<" "<<<<endl;
if(n)
cout<<endl;
continue;
}
else
{
int tl=,tr=;
while()
{
if(l==)
{
tr+=(r-);
break;
}
else if(r==)
{
tl+=(l-);
break;
}
else
{
if(l>r)
{ tl+=l/r;
l=l%r; }
else//l<r
{
// tr++;
tr+=r/l;
r=r%l;
//左边不变
}
}
}
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<tl<<" "<<tr<<endl;;
if(n)
cout<<endl; }
}
}
return ;
}

												

POJ-2499-Binary Tree-思维题的更多相关文章

  1. Poj 2499 Binary Tree(贪心)

    题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...

  2. POJ 2499 Binary Tree

    题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...

  3. POJ 2499 Binary Tree(二叉树,找规律)

    题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...

  4. Balanced Binary Tree——经典题

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...

  6. poj 1032 Parliament 【思维题】

    题目地址:http://poj.org/problem?id=1032 Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  7. LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)

    题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...

  8. CodeForce - 1189 D1. Add on a Tree (思维题)

    Note that this is the first problem of the two similar problems. You can hack this problem only if y ...

  9. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  10. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

随机推荐

  1. WordPress建站要怎样选择适合自己的主机

    目前很多大中小的网站都在使用WordPress进行建站,因为互联网站长都知道WordPress建站是很方便的,简洁的界面,栅格化管理风格,深受互联网站长的喜爱. 现在支持WordPress建站的主机商 ...

  2. python编程语言学习day02

    格式化输出 (1)info 格式 (2)%字符串占位 %s 表示字符串占位 %d 表示整数占位 %f 表示浮点数占位 中间的%     之后是所需要输入的值 多个占位, %  之后用()括号括起    ...

  3. 全球首个开放应用模型 OAM 开源

    业界要闻 全球首个开放应用模型 OAM 开源 2019 年 10 月 17 日,阿里巴巴合伙人.阿里云智能基础产品事业部总经理蒋江伟(花名:小邪)在 Qcon 上海重磅宣布,阿里云与微软联合推出开放应 ...

  4. 视频质量评测标准——VMAF

    阿里云视频云直播转码每天都会处理大量的不同场景.不同编码格式的直播流.为了保证高画质,团队借助VMAF标准来对每路转码的效果做质量评估,然后进行反馈.调优.迭代.这么做的原因在于,像动作片.纪录片.动 ...

  5. DELPHI 异形窗体

    一定有很多人看到过一些奇形怪状的窗体,例如一些屏幕精灵.其实实现起来非常容易,做到三点就好啦.下面我使用Delphi做了一个VCL控件(TBmpShape),你只需要指定一幅图片就可以将窗体变成你的图 ...

  6. 几道51nod上据说是提高组难度的dp题

    1409 加强版贪吃蛇 听着懵逼做着傻逼. 每个格子只能经过一次,穿过上下界答案清0,不考虑穿的话就随便dp.如果要穿就是从尽可能上面的位置穿过上界,尽可能下面的位置穿过下界. 那么转移这一列之前找一 ...

  7. 高可用开源方案Heartbeat vs Keepalived

    转:http://www.kuqin.com/shuoit/20140623/340745.html 最近因为项目需要,简单的试用了两款高可用开源方案:Keepalived和Heartbeat.两者都 ...

  8. Try running RemoteDll as Administrator

    在使用RemoteDll注入动态库的时候发现注入有的动态库会提示下面的错误, LoadLibrary on remote process [1968 - Explorer.exe] failed. T ...

  9. win7 设置双屏壁纸

    http://dualmonitortool.sourceforge.net/ http://www.displayfusion.com/Features/Wallpaper/ 以第一个软件为例: 1 ...

  10. D.Country Meow 最小球覆盖 三分套三分套三分 && 模拟退火

    // 2019.10.3 // 练习题:2018 ICPC 南京现场赛 D Country Meow 题目大意 给定空间内 N 个点,求某个点到 N 个点的距离最大值的最小值.   思路 非常裸的最小 ...