Minimum and maximum
[Problem description]
I have done this question with the greatest product. I believe this question is hard for you.
If you know a number of strings, you can add the multiplication number at the appropriate position (if you add K numbers, you can also add them, that is, divide them into k + 1 parts ), set the product of the k + 1 part (if k = 0, the product is the value of the original number string) to the remainder of M (I .e. mod m) to X;
Returns the minimum value x can reach and the minimum value K in this case, and the maximum value x can reach and the minimum value of K in this case (the minimum value of X can be the same as the maximum value ).
[Input]
The first behavior is a number of strings with a length of N equal to 2 <=n <= 1000, and the number of strings does not exist 0;
The second act is m, meeting 2 <= m <= 50.
[Output]
The four numbers are the minimum values of x and K in this case, and the maximum values of X are separated by spaces.
[Example input]
4421
22
[Sample output]
0 1 21 0
I think this question is quite good.
First you need to know the remainder theorem: (A * B) mod m = (a mod m) * (B mod m) mod m
F [I] [J] indicates the number of the first I. K is used when the remainder is J.
B [I] [J] indicates the mod m value formed from the number of I to the number of J.
F [k] [s] = min {f [I] [J] + 1}
Where S = (B [I + 1] [k] * j) mod m; (Remainder Theorem)
How can we understand this state transition equation? Understand the meaning of each representation.
B [I + 1] [k] is the value of mod m formed from the number I + 1 to the number K.
F [k] [s] indicates the number of the first K, and the remainder is the K used for S. The state transition equation can be understood.
When outputting results,
Because F has been saved to reach the least multiplication number of all the remainder, you only need to resend the maximum remainder, and the minimum number of the output needs to be scanned.
Recommended series: Minimum and Maximum [DP + Remainder Theorem]