当前位置: 首页 > news >正文

OpenJudge | 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 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.

样例输入

3
42 1
3 4
17 73

样例输出

Scenario #1:
41 0Scenario #2:
2 1Scenario #3:
4 6

来源

TUD Programming Contest 2005 (Training Session), Darmstadt, Germany

Code

一开始,我的想法是用广度优先搜索来解决,好吧,其实,也没必要用这个东西来解决

#include <bits/stdc++.h>
using namespace std;struct node {int countL, countR, a, b;
};pair<int,int> solve(int a, int b) {queue<node> q;node f;if(a - b >= 1) q.push({1, 0, a-b, b});if(b - a >= 1) q.push({0, 1, a, b-a});while(!q.empty()) {f = q.front();if(f.a == 1 && f.b == 1) {return make_pair(f.countL, f.countR);}q.pop();if(f.a - f.b >= 1) q.push({f.countL+1, f.countR, f.a-f.b, f.b});if(f.b - f.a >= 1) q.push({f.countL, f.countR+1, f.a, f.b-f.a});}
}int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int N, a, b;cin >> N;for(int i = 1; i <= N; ++i) {cin >> a >> b;pair<int, int> res = solve(a, b);cout << "Scenario #"<< i << ": \n" << res.first << " " << res.second << endl << endl;}
}

毫无疑问,不负众望,直接超时了。

这该怎么办?

你有没有发现一个事情

其实,可以用乘除运算来解决。
a1 = a1 % b1b1 = b1 % a1countLcountR分别是a1 / b1b1 / a1

说是怎么说,但是我们要考虑到一个问题——当a1 % b1或者b1 % a1等于0的时候,我们要对countLcountR进行处理——减1,由于是整除关系,a1b1如果直接使用a1 % b1b1 % a1的话,会变成0,所以我们要对a1b1进行处理——使其变成1即可。

#include <bits/stdc++.h>
using namespace std;
long countL, countR, a1, b1;void solve(long a, long b) {countL = countR = 0;a1 = a;b1 = b;while(a1 != 1 || b1 != 1) {if(a1 > b1) {countL += a1/b1;if(a1 % b1 == 0) {--countL;a1 = 1;} else {a1 = a1 % b1;}} else {countR += b1 / a1;if(b1 % a1 == 0) {--countR;b1 = 1;} else {b1 = b1 % a1;}}}
}int main() {long N, a, b;scanf("%ld", &N);for(long i = 1; i <= N; ++i) {scanf("%ld %ld", &a, &b);solve(a, b);printf("Scenario #%ld:\n%ld %ld\n\n", i, countL, countR);}
}

http://www.mrgr.cn/news/41994.html

相关文章:

  • 1000题-计算机网络系统概述
  • 【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——13.mapset(模拟实现)
  • 解决ModuleNotFoundError: No module named ‘torchcrf‘
  • 【数据结构】红黑树相关知识详细梳理
  • macos vscode+dosbox 8086汇编语言开发调试环境搭建方法 包含所有相关的工具的下载和安装配置方法
  • 前端面试如何说解vue项目性能优化,你确定不来看看吗?
  • strcmp和strncmp
  • 滚雪球学Oracle[4.4讲]:游标管理
  • 【MySQL】DML数据操作语句和基本的DQL语句
  • 函数式编程的优点和缺点
  • 【寻找one piece的算法之路】——双指针算法!他与她是否会相遇呢?
  • 数字中国建“2522”整体框架
  • Java研发笔记6——C语言程序设计学习笔记5
  • CGLIB原理
  • 影刀RPA实战:excel相关图片操作指令解
  • nodejs:实现大文件的分段上传
  • Cpp::STL—vector类的模拟实现(11)
  • Wireshark 解析QQ、微信的通信协议|TCP|UDP
  • 重生之我们在ES顶端相遇第 19 章 - 综合排序(进阶),打造你的个性化排序