HDU4317 Unfair Nim

Source: Internet
Author: User

2012 Multi-University Training Contest 2
1008 questions

The solution report is very unclear and does not know what it wants to express.
Question address: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4317

Question: Find a way to make the number of N stones different or later 0.
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 indicates the number of digits from low to high. j indicates the status after compression, if a bit is 1, it indicates that adding 1 at the same position of the I-1 bit produces carry. 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. Make sure the number of 1 of the I-1 bit 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 j is 1 and c [I] ^ k is 0, 2 is required.
If j is 1 and c [I] ^ k is 1, increase 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 in the place where j has 1 digit.

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 ).

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.

The question was given a time limit of 10 s and ran for 1.5 s. The speed was not too fast.

[Cpp]
# Include <cstdio>
# Include <cstring>
Const int MAXN = 15;
Const int MAXM = 25;
Constint 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 );
IsEven [I] = (num [I] & 1) = 0 );
}
}
 
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 (int 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;
}
Author: CyberZHG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.