Simple Problem
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
On the trial external independent evaluation in mathematics, Zenyk got the following problem.
You are given two positive integers — \(p\) and \(q\). You need to find the smallest positive integer \(n\) such that the numbers \(1, 2, 3, \ldots, n\) can be colored in two colors — red and black such that the sum of the red numbers to the sum of the black numbers is in the ratio \(p\) to \(q\).
Help Zenyk solve this problem.
Input
The single line contains two positive integers — \(p\) and \(q\).
Output
In the first line, output the minimum positive integer \(n\) for which the partition described in the condition exists.
In the second line, output any such partition. If you want to color
the \(i\)-th number red, the \(i\)-th character should be R,
and if black — B.
Note that for the given constraints, an answer not exceeding \(2 \cdot 10^5\) always exists.
Constraints
\(1 \le p \le q\),
5 tests: \(q \le 10\),
5 tests: \(q \le 100\),
15 tests: \(q \le 100000\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 2 4 | 2 RB |
| Input (stdin) | Output (stdout) |
|---|---|
| 4 6 | 4 RBRB |
Notes
In the first example \(p = 2, q = 4\). Then for \(n = 2\) we can color the number 1 red, and the number 2 — black. In this case, the sum of the red numbers will be 1, the black ones — 2, and therefore their ratio will be \(1:2\), which is equivalent to \(2:4\). Clearly, for \(n = 1\) this is impossible to achieve, because at least one of the sums would be equal to \(0\).
In the second example \(p = 4, q = 6\). Then for \(n = 4\) we can color the numbers 1 and 3 red, and 2 and 4 — black. Here the sum of the red numbers will be 4, the black ones — 6, and therefore their ratio will be \(4:6\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|