#P8782. [蓝桥杯 2022 省 B] X 进制减法
[蓝桥杯 2022 省 B] X 进制减法
Background
2025-04-10: The incorrect testdata in the problem was removed.
Problem Description
A base determines when a carry happens on each digit position.
The base is a special kind of base, because the base of each digit position is not fixed. For example, for some -base number, the lowest digit position is base , the second digit position is base , and the third digit position is base . Then the -base number 321 converts to the decimal number 65.
Now there are two integers and written in base, but the exact base of each digit position is still unknown. The only things known are that and follow the same base rule, and that for each digit position, the maximum base is and the minimum base is . Please compute the minimum possible value of .
Note that you must ensure that both and are valid in base, i.e., the digit on each position must be smaller than its base.
Input Format
The first line contains a positive integer , with the meaning as described above.
The second line contains a positive integer , the number of digits of the -base number .
The third line contains integers separated by spaces, representing the digits of in decimal, from the most significant digit to the least significant digit.
The fourth line contains a positive integer , the number of digits of the -base number .
The fifth line contains integers separated by spaces, representing the digits of in decimal, from the most significant digit to the least significant digit.
Note that all numbers in the input are in decimal.
Output Format
Output one line containing one integer, which is the minimum possible value of in base, converted to decimal and then taken modulo (i.e., ).
11
3
10 4 0
3
1 2 0
94
Hint
Sample Explanation
When the bases are: base for the lowest digit, base for the second digit, and base for the third digit, the subtraction gives the minimum difference. In this case, is in decimal, is in decimal, and the difference is .
Constraints and Notes
For of the data, , .
For of the data, , , .
Lanqiao Cup 2022 Provincial Contest B Group, Problem E.
Translated by ChatGPT 5