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 |
|---|
Simple Problem - Editorial
First, reduce the fraction by dividing the numerator and the denominator by their greatest common divisor. Since the ratio of the red sum to the black sum is \(\frac{p}{q}\), the red sum is \(r = p \cdot x\) and the black sum is \(b = q \cdot x\), where \(x\) is an integer. Adding these two sums gives \((p + q) \cdot x\), and on the other hand the sum of the numbers from 1 to \(n\) is \(\frac{n \cdot(n+1)}{2}\). So we need \(n\) such that \(\frac{n \cdot(n+1)}{2}\) is divisible by \(p + q\).
Find the smallest such \(n\). It is not greater than \(2\cdot(p + q)\), since for \(n = 2\cdot(p + q)\) the condition holds. So we can find \(x\), and knowing \(x\), we can find \(r\) and \(b\).
By induction one can prove that the numbers from 1 to \(n\) can make any sum from 0 to \(\frac{n \cdot(n+1)}{2}\), so they can also make the sum \(r\). For this we can take the largest number not taken yet such that the total does not exceed the required sum.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|