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

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 0

Scenario #2:
2 1

Scenario #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.kler.cn/news/331095.html

相关文章:

  • kafka发送消费核心参数与设计原理详解
  • 《pyqt+open3d》open3d可视化界面集成到qt中
  • cloud-(Nacos)--注册中心原理-服务注册-服务发现
  • C# Blazor Server 调用海康H5Player播放摄像头画面
  • STM32 实现 UDP 广播通信
  • 从零到一:编写你的第一个PHP API
  • Spring Boot项目中使用MyBatis
  • 利用vue-capper封装一个可以函数式调用图片裁剪组件
  • 在 Qt 项目中使用 spdlog 的全攻略
  • 【硬件模块】SG90舵机模块
  • Veritus netbackup 管理控制台无法连接:未知错误
  • 【力扣 | SQL题 | 每日三题】力扣1264, 1113, 1098, 1082
  • CSP-J 2023 T1小苹果 T2公路
  • 一、Spring Boot集成Spring Security之自动装配
  • Gazebo安装,ubuntu22
  • Linux云计算 |【第四阶段】RDBMS1-DAY3
  • django创建一个新的应用
  • 什么是 Angular 开发中的 Dumb components
  • PowerBI概述
  • 滚雪球学Oracle[4.3讲]:PL/SQL控制结构与循环的深入解析与优化