Binary Tree
来源:互联网 发布:php curl下载远程图片 编辑:程序博客网 时间:2024/06/06 01:03
9:Binary Tree
- 查看
- 提交
- 统计
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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? - The root contains the pair (1, 1).
- 输入
- 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*109) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above. - 输出
- 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.
- 样例输入
342 13 417 73
- 样例输出
Scenario #1:41 0Scenario #2:2 1Scenario #3:4 6
- 来源
- TUD Programming Contest 2005 (Training Session), Darmstadt, Germany
#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<iomanip>#include<queue>#include<stack>#include<vector>#include<set>#include<map>using namespace std;int main(){int Test,a,b;cin>>Test;for(int i=1;i<=Test;++i){cin>>a>>b;int l=0,r=0;while(a!=1||b!=1){if(a>b){if(a%b==0){l+=(a/b-1);a=1;}else{l+=a/b;a=a%b;}}else{if(b%a==0){r+=(b/a-1);b=1;}else{r+=b/a;b=b%a;}}}cout<<"Scenario #"<<i<<":"<<endl;cout<<l<<" "<<r<<endl<<endl;}return 0;}
阅读全文
0 0
- Binary tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- binary tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- binary tree
- 屏幕亮度调节—基于Android_6.0(代码源于Google)
- Vue笔记(一)
- Beginning Spring学习笔记——第2章(一)Spring IoC容器
- 线性筛法与欧拉函数
- maven与Tomcat包冲突问题
- Binary Tree
- 字符串排序 Java编程练习 学堂在线
- js面试题知识点全解(一原型和原型链1)
- Android-Intent传递数据(Bitmap)闪退问题
- Mysql之Copying to tmp table
- Java web学习笔记
- C# 并行编程 之 Barrier的使用
- 浏览器的缓存机制
- JavaScript基础系列之四 面向对象编程