#P10797. 「CZOI-R1」进制
「CZOI-R1」进制
Problem Description
You have a number , and you need to perform operations on it.
In each operation, you may choose one valid digit of in base and increase its value by .
The first non-zero digit and all digits after it are considered valid digits.
Note:
- For each operation, you may choose any .
- You must ensure that the increment does not cause a carry in the base- representation of .
Now you need to find the maximum possible value of this number after operations.
Output the answer in decimal, modulo . You need to output the result of (the maximum value) modulo , not the maximum value after taking modulo .
Input Format
This problem has multiple test cases.
The first line contains an integer , the number of test cases.
The next lines each contain two integers , representing the initial number and the number of operations.
Output Format
For each test case, output one integer per line, representing the maximum value of after performing operations.
1
2 1
3
Hint
[Sample Explanation]
Clearly, is in binary, and it is in base or higher.
In binary, adding to the first digit would cause a carry, so you can only add to the second digit. The result is , which is in decimal.
In base or higher, you can only add to the last digit. In base it would cause a carry, so discard it. In base or higher, the result is always , and the converted decimal result is also .
[Constraints]
This problem uses bundled testdata.
- Subtask #1 (): .
- Subtask #2 (): .
- Subtask #3 (): no special constraints.
For of the testdata, , and .
Translated by ChatGPT 5