eolymp
bolt
Try our new interface for solving problems
Məsələlər

The Stern-Brocot Number System

The Stern-Brocot Number System

\includegraphics{https://static.e-olymp.com/content/e1/e1c3503ae7f16fb3ef4763f6d139f43b6d44a573.jpg} The Stern-Brocot \textit{tree} is a beautiful way for constructing the set of all nonnegative fractions \textbf{m} / \textbf{n} where \textbf{m} and \textbf{n} are relatively prime. The idea is to start with two fractions and then repeat the following operations as many times as desired: \includegraphics{https://static.e-olymp.com/content/47/4730014c3ea1a4899e5d9c8a8e2202bdf4809aee.jpg} \includegraphics{https://static.e-olymp.com/content/7b/7beaada76fe866970878fe9438e022ef81e65a14.jpg} \includegraphics{https://static.e-olymp.com/content/74/74a99f39474c6bdb89a2919d698259b3d2614e73.jpg} Insert between two adjacent fractions and . \includegraphics{https://static.e-olymp.com/content/9f/9f6c43c38202fbcf6af9aa3f1b31b90af5b9e438.jpg} \includegraphics{https://static.e-olymp.com/content/af/af42dae96dfe4d7a46dd056a0ff40f867f83c45c.jpg} For example, the first step gives us one new entry between and , \includegraphics{https://static.e-olymp.com/content/c1/c18b349ddd2dac8c1250675a912b3ee8de540b59.jpg} and the next gives two more: \includegraphics{https://static.e-olymp.com/content/97/97890f8a6381d44d26196eaf513ce768863ec7ea.jpg} The next gives four more, \includegraphics{https://static.e-olymp.com/content/5b/5b55f9220a6b5b761b926b2d9251918817cbf8cb.jpg} and then we will get \textbf{8}, \textbf{16}, and so on. The entire array can be regarded as an infinite binary tree structure whose top levels look like this: \includegraphics{https://static.e-olymp.com/content/f6/f687a04d50b3314253bc6c39e91e27bc885e08b3.jpg} The construction preserves order, and we couldn't possibly get the same fraction in two different places. \includegraphics{https://static.e-olymp.com/content/db/db004d6b1919193c084711f24cda8130ba249710.jpg} \includegraphics{https://static.e-olymp.com/content/37/37559995fd47faf16859312f6eae5896ae420560.jpg} \includegraphics{https://static.e-olymp.com/content/05/0529c7d0da6a12f58d2b0143da08035980432dc0.jpg} \includegraphics{https://static.e-olymp.com/content/a9/a996a78f1c16bcd08feaa7eb490ffb9fabd3aaf1.jpg} \includegraphics{https://static.e-olymp.com/content/a3/a3a7e3fc934a535c7e8ba9714473c7a7568955f8.jpg} \includegraphics{https://static.e-olymp.com/content/a3/a3a7e3fc934a535c7e8ba9714473c7a7568955f8.jpg} We can, in fact, regard the \textit{Stern-Brocot tree} as a \textit{number system} for representing rational numbers, because each positive, reduced fraction occurs exactly once. Let's use the letters \textit{L} and \textit{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 \textbf{L}'s and \textbf{R}'s uniquely identifies a place in the tree. For example, \textbf{LRRL} means that we go left from down to , then right to , then right to , then left to . We can consider \textbf{LRRL} to be a representation of . Every positive fraction gets represented in this way as a unique string of \textbf{L}'s and \textbf{R}'s. \includegraphics{https://static.e-olymp.com/content/db/db004d6b1919193c084711f24cda8130ba249710.jpg} 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 \textbf{I}, because that looks something like \textbf{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. \InputFile Contains multiple test cases. Each test case consists of a line with two positive integers \textbf{m} and \textbf{n} where \textbf{m} and \textbf{n} are relatively prime. The input terminates with a test case containing two \textbf{1}'s for \textbf{m} and \textbf{n}, and this case must not be processed. \OutputFile For each test case print a line containing the representation of the given fraction in the Stern-Brocot number system.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 128 MiB
Giriş verilənləri #1
5 7
878 323
1 1
Çıxış verilənləri #1
LRRL
RRLRRLRLLLLRLRRR