OpenJudge | Binary Tree

devtools/2024/10/9 15:21:00/

总时间限制: 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.ppmy.cn/devtools/121097.html

相关文章

LSM6DSV16X基于MLC智能笔动作识别(2)----MLC数据采集

LSM6DSV16X基于MLC智能笔动作识别.2--MLC数据采集 概述视频教学样品申请源码下载输出速率执行流程速率设置量程设置检测状态数据单位采集数据静止(Steady)闲置(Idle)书写(Writing)其他(other) 概述 MLC 是“机器学习核心”&#xff08;Machine Learning Core&#xff09;的缩写…

嵌入式外设应用(代码)

文章目录 1. 工业自动化2. 智能家居设备3. 汽车电子4. 生命体征监测仪5. 物联网应用 嵌入式外设应用广泛&#xff0c;有很多应用领域&#xff1a; 1. 工业自动化 应用场景&#xff1a;使用传感器监测设备状态&#xff0c;控制电机的启动和停止。 示例代码&#xff1a; #inc…

SpringBoot实现的师生健康信息管理平台

第1章 绪论 1.1背景及意义 随着社会的快速发展&#xff0c;计算机的影响是全面且深入的。人们生活水平的不断提高&#xff0c;日常生活中人们对医院管理方面的要求也在不断提高&#xff0c;由于老龄化人数更是不断增加&#xff0c;使得师生健康信息管理系统的开发成为必需而且紧…

centos一些常用命令

文章目录 查看磁盘信息使用 df 命令使用 du 命令 查看磁盘信息 使用 df 命令 df&#xff08;disk free&#xff09;命令用于显示文件系统的磁盘空间占用情况。 查看所有挂载点的磁盘使用情况&#xff1a; df -h选项说明&#xff1a; -h 参数表示以人类可读的格式&#xff0…

Flutter---适配高版本studio运行里面的Android项目报错

1. 总是出现错误 可以添加以下命令 在app/build.gradle 里面添加 android {namespace "com.example.untitled"compileSdk flutter.compileSdkVersionndkVersion flutter.ndkVersionconfigurations.all {resolutionStrategy.force com.google.code.findbugs:jsr30…

C(十)for循环 --- 黑神话情景

前言&#xff1a; "踏过三界宝刹&#xff0c;阅过四洲繁华。笑过五蕴痴缠&#xff0c;舍过六根牵挂。怕什么欲念不休&#xff0c;怕什么浪迹天涯。步履不停&#xff0c;便是得救之法。" 国际惯例&#xff0c;开篇先喝碗鸡汤。 今天&#xff0c;杰哥写的 for 循环相…

关于武汉芯景科技有限公司的IIC电平转换芯片XJ9509开发指南(兼容PCa9509)

一、芯片引脚介绍 1.芯片引脚 2.引脚描述 二、系统结构图 三、功能描述 1.VCCA1.35V,VCCB5V,A1输入&#xff0c;B1输出 2.VCCA1.35V,VCCB5V,B1输入&#xff0c;A1输出 3.VCCA1.35V,VCCB5V,A2输入&#xff0c;B2输出 4.VCCA1.35V,VCCB5V,B2输入&#xff0c;A2输出

在C#中使用Redis实现高效消息队列

使用Redis实现C#中的消息队列 Redis是一种开源的内存数据结构存储系统,因其高性能和灵活性被广泛用于缓存、数据库和消息队列等场景。本文将详细介绍如何在C#中使用Redis实现一个简单的消息队列,涵盖环境准备、代码实现和使用示例。 1. 环境准备 1.1 安装Redis 首先,确保…