Question E: rational number tree ------------------------------------ thinking (recursion + simulation)

Title Description In the
nineteenth century, Moriz Stern (1858) and Achille Brocot (1860) invented "a tree". It is said that this tree produced by some simple rules can contain all rational numbers above zero. The tree looks roughly like this:
Insert picture description here

Have you observed the rules?
First, they put two "scores" in the first column. The first one is 0/1, which represents 0; the second one is 1/0, which represents infinity. Then they generate this tree column by column. When they want to generate the k + 1th column, they will first arrange all the scores of the first k columns according to the size (assuming there are n) n-1 interval, then column k + 1 is ready to generate n-1 numbers, the numerator of the value is exactly the sum of the numerators of the left and right numbers, and the denominator is the sum of the denominators of the left and right numbers.
For example, 2/3, and its 2 is the result of the addition of 1 on the left 1/2 and the molecule 1 on the right 1/1; and 3 of 2/3 is 1/2 of 2 plus 1/1 The denominator is 1.
From this tree, we can see that each positive minimal score appears exactly once in this tree, and we use the letters "L" and "R" to denote a step from the root (1/1) of the tree. "Go left" and "Go right", then each number can be represented by a sequence of L and R.
For example, LRRL means to start one step from 1/1 to the left to 1/2, then to the right to 2/3, then to the right to 3/4, and finally to the left to 5/7. We can think of LRRL as a representation of 5/7. Almost every positive score has a unique way to be expressed as a sequence of L and R.
Given a score, output its LR notation.

Input The
input has two mutually prime positive integers m and n (1 ≤ n, m ≤ 1000).
Output
Output the corresponding LR notation.
Sample input Copy
5 7
Sample output Copy
LRRL

Resolution:

Nature: Binary sorting tree left subtree is smaller than root right subtree is larger than root

Simulation based on the above properties. Store the numerator denominator, left numerator denominator, and right numerator denominator into the structure, and then process it recursively

#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N=1e5+10000;
struct node
{
    int l1,l2,r1,r2,fz,fm;
}a[N];
string s;
void dfs(int i)
{
    double x=(a[i].fz*1.0/a[i].fm*1.0);
    double y=(n*1.0/m*1.0);
    if(y<x)
    {
        a[i+1].fz=a[i].l1+a[i].fz;
        a[i+1].fm=a[i].l2+a[i].fm;
        a[i+1].l1=a[i].l1;
        a[i+1].l2=a[i].l2;
        a[i+1].r1=a[i].fz;
        a[i+1].r2=a[i].fm;
        s+='L';
        dfs(i+1);
    }
    else if(y>x)
    {
        a[i+1].fz=a[i].fz+a[i].r1;
        a[i+1].fm=a[i].fm+a[i].r2;
        a[i+1].l1=a[i].fz;
        a[i+1].l2=a[i].fm;
        a[i+1].r1=a[i].r1;
        a[i+1].r2=a[i].r2;
        s+='R';
        dfs(i+1);
    }
     
}
int main()
{
    scanf("%d %d",&n,&m);
    a[0].l1=0;a[0].l2=1;a[0].r1=1;a[0].r2=0;a[0].fz=1;a[0].fm=1;
    dfs(0);
    cout<<s<<endl;
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105244809
Recommended