CF1593F.Red-Black Number

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

It is given a non-negative integer xx, the decimal representation of which contains nn digits. You need to color each its digit in red or black, so that the number formed by the red digits is divisible by AA, and the number formed by the black digits is divisible by BB.

At least one digit must be colored in each of two colors. Consider, the count of digits colored in red is rr and the count of digits colored in black is bb. Among all possible colorings of the given number xx, you need to output any such that the value of ∣r−b∣|r - b| is the minimum possible.

Note that the number xx and the numbers formed by digits of each color, may contain leading zeros.

Example of painting a number for A=3A = 3 and B=13B = 13

The figure above shows an example of painting the number x=02165x = 02165 of n=5n = 5 digits for A=3A = 3 and B=13B = 13. The red digits form the number 015015, which is divisible by 33, and the black ones — 2626, which is divisible by 1313. Note that the absolute value of the difference between the counts of red and black digits is 11, it is impossible to achieve a smaller value.

给定一个非负整数 xx,其十进制表示包含 nn 位数字。你需要将 xx 的每一位数字染成红色或黑色,使得所有红色数字按原顺序组成的数能被 AA 整除,所有黑色数字按原顺序组成的数能被 BB 整除。

每种颜色(红与黑)都至少需染一个数字。设红色数字的个数为 rr,黑色数字的个数为 bb。在所有满足条件的染色方案中,你需要输出任意一种使得 ∣r−b∣|r - b| 尽可能小的方案。

注意:整数 xx 以及由每种颜色的数字所组成的数均允许包含前导零。

当 A=3A = 3 且 B=13B = 13 时对一个数字进行染色的示例

上图展示了对 n=5n = 5 位数字 x=02165x = 02165 进行染色的一个示例(对应 A=3A = 3,B=13B = 13)。红色数字组成数字 015015,可被 33 整除;黑色数字组成数字 2626,可被 1313 整除。注意,红色与黑色数字个数之差的绝对值为 11,这是无法进一步减小的。

输入格式

The first line contains one integer tt (1≤t≤101 \le t \le 10) — the number of test cases. Then tt test cases follow.

Each test case consists of two lines. The first line contains three integers nn, AA, BB (2≤n≤402 \le n \le 40, 1≤A,B≤401 \le A, B \le 40). The second line contains a non-negative integer xx containing exactly nn digits and probably containing leading zeroes.

第一行包含一个整数 tt(1≤t≤101 \le t \le 10)——测试用例的数量。随后是 tt 个测试用例。

每个测试用例由两行组成。第一行包含三个整数 nn、AA、BB(2≤n≤402 \le n \le 40,1≤A,B≤401 \le A, B \le 40)。第二行包含一个非负整数 xx,该整数恰好有 nn 位数字,且可能包含前导零。

输出格式

For each test case, output in a separate line:

  • -1 if the desired coloring does not exist;
  • a string ss of nn characters, each of them is a letter 'R' or 'B'. If the ii-th digit of the number xx is colored in red, then the ii-th character of the string ss must be the letter 'R', otherwise the letter 'B'.

The number formed by digits colored red should divisible by AA. The number formed by digits colored black should divisible by BB. The value ∣r−b∣|r-b| should be minimal, where rr is the count of red digits, bb is the count of black digits. If there are many possible answers, print any of them.

对于每个测试用例,在单独的一行中输出:

  • 若不存在满足要求的染色方案,则输出 -1;
  • 否则输出一个长度为 nn 的字符串 ss,其中每个字符为字母 'R' 或 'B'。若数字 xx 的第 ii 位数字被染成红色,则字符串 ss 的第 ii 个字符必须是 'R',否则为 'B'。

由红色数字构成的数应能被 AA 整除;由黑色数字构成的数应能被 BB 整除。令 rr 表示红色数字的个数,bb 表示黑色数字的个数,则 ∣r−b∣|r-b| 应尽可能小。若存在多个满足条件的答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    5 3 13
    02165
    4 2 1
    1357
    8 1 1
    12345678
    2 7 9
    90

    输出#1

    RBRBR
    -1
    BBRRRRBB
    BR

说明/提示

The first test case is considered in the statement.

In the second test case, there are no even digits, so it is impossible to form a number from its digits that is divisible by 22.

In the third test case, each coloring containing at least one red and one black digit is possible, so you can color 44 digits in red and 44 in black (∣4−4∣=0|4 - 4| = 0, it is impossible to improve the result).

In the fourth test case, there is a single desired coloring.

题目陈述中已考虑第一个测试用例。

在第二个测试用例中,不存在偶数数字,因此无法用其数字构成一个能被 22 整除的数。

在第三个测试用例中,所有至少包含一个红色数字和一个黑色数字的染色方案均是可行的,因此你可以将 44 个数字染成红色、44 个染成黑色(此时 ∣4−4∣=0|4 - 4| = 0,结果已无法进一步优化)。

在第四个测试用例中,仅存在一种满足要求的染色方案。

输入解题思路,AI测评打分。不知道怎么写?

首页