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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mryan2005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值