Http: // 162.105.81.212/judgeonline/problem? Id = 3372
There is a magical formula for this question. There is nothing to say. The following content is changed from lonelyboy to sunhaowen.
N Children are circled in a circle. The teacher is clockwise separated by 0, 1, 2, 3 ,... A child sends sugar and asks if every child can receive sugar.
The solution is simple. If n is 2 ^ K, yes is output; otherwise, no is output. Printf (N & n-1 )? "No/N": "Yes/N ");
How can we prove the key. The Discuss statement is related to the remaining two times, but it is not shown... The following is a simple proof.
If n is an odd number, take 7 as an example. When the increment is greater than N, mod n does not affect the increment.
Increment 0 1 2 3 4 5 6 0 1 2 3 4 5 6
No. 0 1 3 6 3 1 0 1 3 6 3 1 0
You will see that this series is repeated, and the size of the repetition interval is 7. Then, observe [0, 6]. You will find that the repetition interval is symmetric. Why?
Because the essence of 3 + 4 = 7,0 + 1 + 2 + 3 + 4 is 0 + 1 + 2, so when n is an odd number, its repetition interval is symmetric, so we can know that, he can only send sugar to (n + 1)/2 children at most, and 0, 1, 3, 6, and 4 children at 7.
Therefore, when n is an odd number, it must be no.
What kind of phenomenon does n have when n is an even number? Take 10 as an example:
Increment 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
No. 0 1 3 6 0 5 1 6 5 5 6 8 1 5 6 3 1 0
You will find that it appears in a 2 * n repeating interval. Why? Because 1 + 9 = 10, 2 + 8 = 10...
So 0 + 1 +... 9% 10 = 5. 0 + 1 +... N-1% n = n/2. When the two intervals are combined, the return value is 0.
And 0 +... (n-1) + 0 + 1 = 0 +... (N-2), in the same way as an odd number, the interior of the repetition interval is symmetric.
How can we use this symmetry again? We found that the numbers are actually steps n/2 starting from 0, and n/2 starting from n/2.
Solve the problem based on the symmetry of number 0 and number n/2 on the ring.
First look at the path L1 generated in step n/2 of 0, which is 0 1 3 6 0. Here, an element 6 greater than n/2 is displayed.
Let's look at the path L2 of n/2, which is 5 6 8 1 5. Here an element 1 smaller than n/2 appears.
While 6% 5 = 1. This is due to the symmetric relationship between 0 and n/2.
When the id j of position I in path L1 of 0 is greater than n/2, the ID of position I in L2 must be J % (n/2 ).
Then we can modify the two paths so that they do not infringe upon each other, so that all values on L1 are MoD (n/2). If L1 can cover 0 to n/2-1, then L2 can overwrite n/2 to n-1.
Then L1 is essentially converted into a smaller problem: F (n/2 );
Write Recursion
| Yes (n = 1 );
F (n) = | f (n/2) (N & 1 = 0)
| No (N & 1 = 1)
So we can see that N is only 2 ^ K output yes.
# Include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> int N; <br/> while (scanf ("% d", & N )! = EOF) <br/>{< br/> while (n) <br/>{< br/> If (N & 1 & n! = 1) <br/> break; <br/> N >>=1; <br/>}< br/> If (N) <br/> printf ("No/N"); <br/> else <br/> printf ("Yes/N "); <br/>}< br/> return 0; <br/>}