#P15402. [NOISG 2026 Prelim] Digits
[NOISG 2026 Prelim] Digits
Problem Description
Jayden is a math nerd who is obsessed with numbers! His favourite number is an -digit string . Ziv gives him other -digit strings . All digits in these strings (including ) range from 0 to , where is a given integer (). Let denote the -th digit of from the left.
As Jayden loves his favorite number so much, he wishes to turn all the numbers that Ziv gave him into the number using a number transformation machine. An operation on works as follows:
- Choose two integers and where .
- For every , set the value of to .
is a given array of length . denotes the remainder of dividing by (for example, ). The cost of this operation is dollars (in particular, if , the cost is ), where is a given array of length . Refer to the sample test cases for more details.
For each , help Jayden solve the following problem independently:
- What is the minimum total cost needed (in dollars) to transform into the number using any number of operations?
If it is impossible to transform into , output instead.
Input Format
Your program must read from standard input.
The first line of input contains three space-separated integers , , and .
The second line of input contains space-separated integers .
The third line of input contains space-separated integers, .
The fourth line of input contains one integer .
The next lines of input each contain one integer. The -th of these lines contains .
Output Format
Your program must print to standard output.
The output should contain lines, each containing one integer. The -th of these lines should contain the minimum total cost needed to transform into . If this is impossible, output instead.
6 3 8
1 2 3
3 1 4
676
356
431
676
767
133
715
16
42
0
-1
25
37
3 4 2
1 1 1 1
1 1 1 1
1001
1110
1100
0110
2
4
2
1 1 10
1
67
6
7
1206
1 2 10
1 1
1 1000000000
24
83
1000000007
Hint
Sample Test Case 1 Explanation
Jayden’s favourite number is .
Consider . The following sequence of 3 operations can transform 356 into 676:
$$\underline{356} \xrightarrow{l=1,\ r=2} \underline{476} \xrightarrow{l=1,\ r=1} \underline{576} \xrightarrow{l=1,\ r=1} \underline{676}$$-
: The 1-st and 2-nd digits become and respectively. This costs dollars.
-
: The 1-st digit becomes . This costs dollars.
-
: The 1-st digit becomes . This costs 6 dollars.
The total cost of the three operations is dollars. It can be shown that there is no other sequence of operations that incurs a lower total cost.
For , no operations have to be made as the number is already equal to . Hence, the minimum total cost is 0 dollars.
For , it can be shown that there is no sequence of operations that can transform 767 into 676. Hence, output .
Subtasks
For all test cases, the input will satisfy the following bounds:
- for all
- for all
- are all -digit strings, where each digit ranges from 0 to inclusive. They may contain leading zeros.
Your program will be tested on input instances that satisfy the following restrictions:
| Subtask | Score | Additional Constraints |
|---|---|---|
| 0 | Sample test cases | |
| 1 | 5 | and for all |
| 2 | 13 | and for all |
| 3 | 10 | and |
| 4 | 16 | |
| 5 | 24 | |
| 6 | 32 | No additional constraints |