[Description]
When FF arrived at the temple, the temple was broken. But there is a new stone in the center of the temple. Small FF approached the stone platform and found a number of strings on the stone platform, while the top of the string was engraved with a string of ancient runtimes. The little FF who is proficient in the ancient rune understands the meaning of the article without any effort. The general idea is: for a string of numbers on the stone platform, you can add a multiplication number (K is added, but not K or 1) in a proper position ), 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 ).
Xiao FF also knows that if he finds the correct answer, he can go to the lower layer of the temple. However, this problem does not seem to be well solved. Mr. FF finds you and promises to give you points after finding the treasure (of course you take two ......).
[Input format]
The first behavior is a number string, and there is no 0 in the number string;
Second act M.
[Output format]
The four numbers are respectively the minimum value of X and K in this case, and the maximum value of X is adjacent to k in this case.
Separate Two numbers with a space.
[Input example]
4421
22
[Output example]
0 1 21 0
[Data Scope]
For 30% of data: 2 <= string length L <= 50.
For 100% of data: 2 <= string length L <= 1000; 2 <= m <= 50.
This question looks like the question with the maximum product, but we know from the question with the minimum mod 4 that the DP with the maximum product is absolutely wrong.
We use F [I, j] to represent the smallest part of the Division required by the remainder J by adding a multiplication number (that is, adding a multiplication number plus 1 ). Then, the equation f [I, K * d [J + 1, I] = min {f [j, k]} + 1 (I> J) is obtained ). Where, the d [I, j] array indicates the result of mod m from I bit to J bit in the string.
Reference code:
Program chars; const maxn = 32767; var A: array [0 .. 1010] of integer; D: array [0 .. 1010, 0 .. 1010] of integer; F: array [0 .. 1010, 0 .. 1010] of integer; V: array [0 .. 50] of Boolean; Ch: Char; I, j, P, MM: longint; n, m, K, sum: longint; begin read (CH ); while (ord (CH)> = 48) and (ord (CH) <= 57) Do begin Inc (n); A [n]: = ord (CH)-48; read (CH); end; readln (m); for I: = 1 to n do for J: = I to n do d [I, j]: = (d [I, J-1] * 10 + A [J]) mod m; for I: = 0 to n + 1 do for J: = 0 to M do f [I, j]: = maxn; // calculates the minimum value. Do not forget to initialize fillchar (v, sizeof (V), false ); f []: = 0; for I: = 1 to n do for J: = 0 to I-1 do for K: = 0 to S-1 do if f [J, k] + 1 <F [I, K * d [J + 1, I] mod m] Then begin f [I, K * d [J + 1, i] mod m]: = f [j, k] + 1; V [K * d [J + 1, I] mod m]: = true; end; for I: = 0 to M-1 do if f [N, I] <> maxn then begin write (I, '', F [N, I]-1,''); break; end; for I: = M-1 downto 0 do if f [N, I] <> maxn then begin write (I, '', F [N, I]-1, ''); break; end.
BTW, Yanni's with an orchid is really good, it's worth hearing!
Address: http://www.cnblogs.com/saltless/archive/2010/11/09/1872277.html
(Saltless original, reprinted please indicate the source)