Binary Tree
来源:互联网 发布:yum配置本地源 编辑:程序博客网 时间:2024/05/21 11:36
Description
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:
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?
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*109) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
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
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.
我们可以通过一个逆向的思维去解决这道题,起点就是输入的数据,终点就是根节点!这个时候我们就可以从测试数据里找一些规律,我们不难发现使用辗转相除就能解决这个问题!
private void campute(int x, int y) { int left = 0; int right = 0; int up; while (x > 1 || y > 1) { if (x > y) { up = (x - 1) / y; left += up; x -= up * y; } else { up = (y - 1) / x; right += up; y -= up * x; } } System.out.println(left + " " + right); } public void getResult() { Scanner scan = new Scanner(System.in); int cases = scan.nextInt(); for (int i = 1; i <= cases; ++i) { int x = scan.nextInt(); int y = scan.nextInt(); System.out.println("SET " + i); campute(x, y); } }
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 IM实践
- 上拉电阻与下拉电阻的作用和区别
- 博弈论-美女的硬币
- django操作数据库
- 记12号腾讯清理日活动有感
- Binary Tree
- 图解SQL Server 2008入门总结
- Rotate Array(3 ways)
- 多核初始化和启动过程
- ExtJs表单几种验证与扩展
- Spark集群安装和使用
- Linux java以及android环境的配置和注意事项
- 谈下以前曾做的一个项目,关于微博的咯
- 本地应用程序利用socket(AF_INET)通信的数据在内核中的流向