The Stern-Brocot Number System(树的左边还是右边)

The Stern-Brocot Number System

Input: standard input

Output: standard output

The Stern-Brocot tree is a beautiful way for constructing the set of all nonnegative fractions m / n where m and n are relatively prime. The idea is to start with two fractions and
then repeat the following operations as many times as desired:

Insert between two adjacent fractions and .

For example, the first step gives us one new entry between and ,

and the next gives two more:

The next gives four more,

and then we will get 8, 16, and so on. The entire array can be regarded as an infinite binary tree structure whose top levels look like this:

The construction preserves order, and we couldn‘t possibly get the same fraction in two different places.

We can, in fact, regard the Stern-Brocot tree as a number system for representing rational numbers, because each positive, reduced fraction occurs exactly once. Let‘s use the letters L and R to
stand for going down to the left or right branch as we proceed from the root of the tree to a particular fraction; then a string of L‘s and R‘s uniquely identifies a place in the tree. For example, LRRL means that we go left from down
to , then right to , then right to ,
then left to . We can consider LRRL to be a representation of . Every
positive fraction gets represented in this way as a unique string of L‘s and R‘s.

Well, actually there‘s a slight problem: The fraction corresponds to the empty string, and we need a notation for that. Let‘s agree
to call it I, because that looks something like 1 and it stands for "identity".

In this problem, given a positive rational fraction, you are expected to represent it in Stern-Brocot number system.

Input

The input file contains multiple test cases. Each test case consists of a line contains two positive integers m and n where m and n are relatively prime. The input terminates with a test case
containing two 1‘s for m and n, and this case must not be processed.

Output

For each test case in the input file output a line containing the representation of the given fraction in the Stern-Brocot number system.

Sample Input

5 7

878 323

1 1

 

Sample Output

LRRL

RRLRRLRLLLLRLRRR

题目大意:

求出给出数字在每一层树枝上的左边还是右边。

解题思路:

数的左边越来越小,右边越来越大,中间的1是分界点。

代码:

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main(){
    int m,n,summ,sumn;
    while(cin>>m>>n&&(m!=1||n!=1)){
       string ans;
       int m0=0,m1=1;
       int n0=1,n1=0;
       while(1){
          summ=m0+m1;
          sumn=n0+n1;
          int temp=m*sumn-n*summ;
          if(temp>0){
              ans+='R';
              m0=summ;
              n0=sumn;
          }
          else if(temp==0) break;
          else{
              ans+='L';
              m1=summ;
              n1=sumn;
          }
       }
       cout << ans << endl;
    }
    return 0;
}
时间: 2024-10-23 16:46:55

The Stern-Brocot Number System(树的左边还是右边)的相关文章

F - The Fun Number System(第二季水)

Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit

Find n’th number in a number system with only 3 and 4

这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 343

Hdu1394Minimum Inversion Number线段树

这个网上一搜一大堆,就是先求一个,其余的for一遍搞出来. #include<stdio.h> #include<stdlib.h> #define max 5555 int sum[max * 4]; int min(int a, int b) { if (a>b) return b; else return a; } void fuqin(int a) { sum[a] = sum[a * 2] + sum[a * 2 + 1]; } void build(int l,

Minimum Inversion Number(线段树单点更新+逆序数)

Minimum Inversion Number(线段树单点更新+逆序数) Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

hdu 4081 Qin Shi Huang&#39;s National Road System 树的基本性质 or 次小生成树

During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all

hdu 1394 Minimum Inversion Number 线段树 点更新

// hdu 1394 Minimum Inversion Number 线段树 点更新 // // 典型线段树的单点更新 // // 对于求逆序数,刚开始还真的是很年轻啊,裸的按照冒泡排序 // 求出最初始的逆序数,然后按照公式递推,结果就呵呵了 // // 发现大牛都是用线段树和树状数组之类的做的,而自己又在学 // 线段树,所以就敲了线段树. // // 线段树的节点保存一段区间( L,R )内0,1...n一共出现了多少个. // 因为每个数是0,1,2...n-1且没有重复的数字. /

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP