poj3210: the gain of very easy problem

Is this the easiest problem in poj? Enter a number, even numbers have no solution, odd numbers are reduced by one output.

Intention:
Snoopy has n coins. After x flips, all coins can be front or back facing up, and you need to get the minimum value of x. (That is to flip x times, so that you can achieve the goal in different situations. In some cases, you only need fewer times, but you must also ensure that the target is achieved after x times)

Analysis:
1 If there are even numbers of coins, there are odd numbers of coins up (down) + odd number of coins down (upper) and even number of coins up (down) + even number of coins down (up). If it is the former, Snoopy can make all the coins unified direction after an odd number of flips. If it is the latter, Snoopy can make all the coins unify direction after even flippings.
For example, if there are 4 coins now, the initial statement is 0001, 0011, 0111, … (0 for downwards and 1 for upwards). If it is 0001, Snoopy can be flipped 1 or 3 times; if it is 0011, Snoopy can be flipped twice to achieve; if it is 0111, Snoopy can be flipped 3 times or 1 time to achieve. Assuming that the value of x is now 3, then the initial statement is 0001, the coin of 0111 can be realized, but if it is 0011, after flipping twice, the coin has already met the target, and then it will be the third time. Therefore, it will break the statement. In summary, when n is even, there will be no exact x value to achieve the target.

2 If there are odd numbers of coins, there are even cases of up (down) + odd down (up). Then Snoopy can make all the coins meet the target state after an even number of flips.
For instance, now there are 5 coins, the initial statement is 00001,00011, … If it is 00001, Snoopy can flip 1 or 4 times; if it is 00011, Snoopy flip 2 or 3 times to achieve the target. If we find the minimum value of x, we can see if the state with the maximum can satisfy other statements. Here we can see that the time of flips is up to 4, the statement is 00001, we look at the other state of 00011, we can achieve 00011 -> 00000 for two times, the remaining two times can be used to flip the same coin to achieve the target. The target statement is not destroyed here because we can always achieve the target through an even times of flips, and then we can flip the same coin to keep the target state after the even number of flips (a coin flips even times is not the original state) _).

To sum up, when n is even, the answer is “No Solution!”; when n is odd, the answer is x = n - 1.

AC point: be careful for the output, such as there may be typing errors in “No Solution!”.

记得双击加关注哦❤

————————————————
版权声明:本文为CSDN博主「alex_yuqian」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/alex_yuqian/article/details/100168848

猜你喜欢

转载自blog.csdn.net/alex_yuqian/article/details/100169952