I always think that the pressure is a violent search. In fact, this is also a violent search... Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4317
Question: give you n numbers. You can add a value to each number to make the difference or value of the last n number 0. Of course, the sum and minimum of the numbers are required, in the past, there was a small game blind eye.
N is very small, and State compression starts with N. All the choices of the N heap are represented in binary.
Based on the exclusive or nature, as long as the numbers of exclusive or correspond to an even number of 1 bits in the same binary, the result of this exclusive or is 0.
DP [I] [J]: I represents the nth bit from low to high, and J is the State after I bit compression, indicating the carry of I bit. DP [I] [J] indicates the minimum number of stones required to reach such a state.
In this definition, if the carry J of the highest bit I can reach has an even number of 1, it indicates that this is an option of the answer. You only need to select the smallest of these values.
The relationship between States is considered below.
The I state is only possible from the state of the I-1. We use C [I] to record the initial state of each number of stones. If the I position of the K stones is 1, then the K-bit of C [I] is 1.
We use num [x] to represent the number of 1 in X, and iseven [x] to indicate whether the number of 1 in X is an even number.
For a J, it is obtained by K in the I-1 State. To reach J, K must meet the following conditions:
1. Ensure that K can reach J
If one of C [I] and K is 1, a carry is generated in this bit, and J must be 1 in this bit, therefore, there is a constraint: (j | (C [I] & K) = J)
2. Ensure that the number of I-bit 1 is an even number.
Because J is the result of the addition of State C [I] and K binary, the number of C [I] ^ K corresponds to a bit,
If it is 0, 2 is required to generate carry, and the result is still 0.
If the value is 1, 1 is required to generate carry, and the result is 0.
Therefore:
If the corresponding J bit is 0: nothing needs to be done.
If the J ing is 1 and the C [I] ^ K ing is 0, you need to add 2 or 0.
If J is 1 and C [I] ^ K is 1, add 1 to 0.
In the end, as long as it is an even number, or there are still 0 digits, we can fill in one stone and we will surely be able to obtain an even number,
Because the bit where J has 1 must be 0 after the carry is generated, so:
It is an even number: iseven [(~ J) & (c [I] ^ K)], the number of the remaining 1 after the first bit of J is an even number.
There are also idle 0 bits: J | num [(C [I] ^ K) | j] <n, because as long as there is J, there will be 0, or 0 exists except where J has 1 digit (j = 1, I ^ K = 0 | j = 0, I ^ K = 0)
The general determination is: (iseven [(~ J) & (c [I] ^ K)] | j | num [(C [I] ^ K) | j] <n)
3. Status K reaches status J
According to the above, we have found K that meets the conditions. Next we need to consider the number of stones.
First, there are already bits in place. The corresponding bits of C [I] and K are both 1, and the bits such as (c [I] & K) do not need to be considered.
Then consider whether to add a stone to meet the even number conditions. If yes, add 1.
Next, the number of 1 in (J & (c [I] ^ K, you need to add an equivalent number of stones to this one.
Finally, when the ing of C [I] ^ K is 0, that is (J ^ (J & (c [I] ^ K ))) remove the number of 1 in (c [I] & K). You need to add two times the number of stones to this position.
Because it is the second I-1, the number of stones required to multiply by 1 <(I-1 ). // It seems like this is a very god. In this case, it is required that J be 1, C [I] ^ K be 0, and C [I] & K be ignored.
To sum up:
DP [I] [J] = min (DP [I] [J], DP [I-1] [k] + (! Iseven [(~ J) & (c [I] ^ K)] + num [J & (c [I] ^ K)] + (Num [J ^ (J & (c [I] ^ K)]-num [C [I] & K]) <1 )) * (1 <(I-1 )));
Boundary Condition:
DP [0] [0] = 0; other values are INF.
For multi-heap scenarios, we can find a way to establish the game. Only a heap of games will cause a certain loss.
When N = 1 and the number is not 0, this is impossible to meet the conditions.
# Include <cstdio> # include <cstring> const int maxn = 15; const int maxm = 25; const int INF = 1000000000; int n, m, a [maxn]; int C [maxm], DP [maxm] [1 <maxn]; int num [1 <maxn]; bool iseven [1 <maxn]; inline int min (int x, int y) {return x <Y? X: Y;} inline int getonenumber (int x) {int result = 0; while (x) {result + = x & 1; X >>=1 ;} return result;} void Init () {for (INT I = 0; I <(1 <maxn); ++ I) {num [I] = getonenumber (I ); // how many iseven [I] = (Num [I] & 1) = 0) in this status ); // The number of status 1 is} int main () {Init (); While (~ Scanf ("% d", & N) {for (INT I = 0; I <n; ++ I) scanf ("% d ", & A [I]); If (n = 1) {if (a [0]) printf ("impossible \ n "); else printf ("0 \ n");} else {memset (C, 0, sizeof (c); For (INT I = 1; I <maxm; ++ I) {for (Int J = 0; j <n; ++ J) {if (a [J] & (1 <(I-1 ))) {c [I] | = (1 <j) ;}if (C [I]) {M = I + 1 ;}}for (INT I = 0; I <= m; ++ I) {for (Int J = 0; j <(1 <n); ++ J) {DP [I] [J] = inf ;}} DP [0] [0] = 0; For (INT I = 1; I <= m; ++ I) {for (I Nt j = 0; j <(1 <n); ++ J) {for (int K = 0; k <(1 <n); ++ K) {If (DP [I-1] [k]! = Inf) {If (j | (C [I] & K) = J) {If (iseven [(~ J) & (c [I] ^ K)] | j | num [(C [I] ^ K) | j] <n) {DP [I] [J] = min (DP [I] [J], DP [I-1] [k] + (! Iseven [(~ J) & (c [I] ^ K)] + num [J & (c [I] ^ K)] + (Num [J ^ (J & (c [I] ^ K)-num [C [I] & K]) <1 )) * (1 <(I-1) ;}}}} int ans = inf; For (INT I = 0; I <(1 <n ); ++ I) {If (iseven [I]) {ans = min (ANS, DP [m] [I]) ;}} printf ("% d \ n ", ans) ;}} return 0 ;}