Summer NOIP Final Exam "1"--phantom

Source: Internet
Author: User

Phantom

Title Name: Phantom
? time limit: 1 seconds
? Space limitations: The MiB

Title Description

On an infinitely large chessboard, n * n pieces are arranged to form a square of n rows n columns. Chess pieces can be moved horizontally or vertically, moving over an adjacent piece, falling into the next free grid in the same direction, and removing the pieces that have been crossed. Now, we want to know if it's possible to pass several operations, leaving only one piece on the board.
For example, when n = 2 o'clock, there are several ways to do this:
.... .... .... ....
. Oo. = ... O = ... O = ....
. Oo. . Oo. ... O....
.... .... .... ... O

Input format

The first line of the input file contains an integer that represents the number of test data.
Each test data occupies one row, containing a separate integer n.

Output format

For each test data, output the answer in a separate line.
If it is possible that there is only one piece left on the board, output YES, otherwise output NO.

Sample Input 2

2
3

Sample Output Yes

NO

Data scope and conventions

? For 30% of data, n≤6.
? For 100% of data, N≤1 000 000 000, test data no more than 1000 groups.

For this question many people initially feel is search, but look at this big data, N<=1 000 000 000, how possible. can only be O (1) time complexity ah. So, intuition tells me, or the individual will know, this problem is a regular to follow the question.

All jokes aside, first of all, the teacher introduced the first method:

For a n=2 4*4 matrix, we can easily turn it into what we want, so it can be used as a basic model, or let it become our desired state, which is similar to the "inductive method" in mathematics (which can be well understood).
After the above analysis, now we can practice a bit. What's the word? "failure is the only way to success", so you will find that when n=3, how can not be achieved. When you were painting 4*4, you might have a complete 2*2 in a situation, and that's exactly what you want.

And we know that there's only one point left, and it's okay, so the same thing happens when n=5:

This time probably will find some rules, that is, we are every three three to eliminate, it seems as long as there are three consecutive can be removed, but in fact, it is not so simple, the condition is to appear an ' L ' shape, that is, the three points at the ends of the two sides at least one point, so that the three points can be eliminated And we have 4,5 these two examples to summarize all the situations (it turns out, don't ask me why, I know it by myself).

Like this, for arbitrary n (and not arbitrary), it can be converted to 4 or 5, which translates to our basic model, 1 or 2.
But here, maybe you have some questions, maybe you're still asking why, because we crossed a very magical number, 3.
Because, if according to the previous thought, then three is not supposed to be gone? --but of course it's impossible, why? Again the fancy language also cannot tell the essence of the thing, so this kind of question still needs to practice for oneself, this naturally will understand.
This time can be extended to any N, for a (n^2) can not be divisible by 3 number, we will deal with, and can be divisible by 3 of the number of the final can not be converted to 1 or 2, so not.

ex--Method 2
I've said it before, it's a magical thing. In a computer, XOR is a kind of bit operation, and it can be calculated quickly using binary. However, we do not use this property in this problem, but the following three kinds of relations:

1^2=3
2^3=1
3^1=2

In other words, the different of the same or the conversion.
So that we can label each of the squares
Taking n=6 as an example

1 2 3 1 2 3
3 1 2 3 1 2
2 3 1 2 3 1
1 2 3 1 2 3
3 1 2 3 1 2
2 3 1 2 3 1
Whatever you find in this matrix, what I want to tell you is that there are two different numbers at the top and bottom of each point, so what are the benefits? Think about it, what is our final state? 1 or 2 or 3, and we know that the total value is 0, so no matter how we move it, we can't have a single number. This conclusion is exactly the same as the one in the first method, which is divisible by 3.
PS: The nature of the different or is very important, understand the profound words for the problem is very useful, if you can think of a problem can be used such as different or the same bit arithmetic, you are God Ben.

The most vivid method--Method 3

With the most essential idea, oo*->**o is the fundamental of our movement, then whether we can abstract a model (take 5 as an example):

Why did you do it? You can see that when I move one step at a time, each point of the color will be added one or one less. If this can be understood, then I say their parity will change, and at the same time become the opposite state , according to:
Blue:8
Black:8
While:9
That is, "even odd", and our goal state is "001", "even odd". Obviously, as long as my black, blue and white numbers are not equal. This makes it easy to understand why it is not possible to divide by 3, because the number of black-and-blue-white colors is always equal in the 3*3 lattice, so this approach solves the confusion in our Method 1. So it is good to understand why n%3==0 can, else not.

Code:
It's too simple to put on.

Summary:

Do the problem before you must look at the data range, often the data range can prompt you to use what algorithm to answer, at the same time the idea can not be rigid, do not want to the template problem, the inspiration may be in the lightning flint between.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summer NOIP Final Exam "1"--phantom

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.