Hdu5088 -- revenge of Nim II (Gaussian elimination element & Matrix Rank) (bestcoder round #16)

Source: Internet
Author: User

Revenge of Nim II


Problem description
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. on each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.
--- Wikipedia
Today, Nim takes revenge on you, again. as you know, the rule of Nim game is rather unfair, only the Nim-sum (percentage) of the sizes of the heaps is zero will the first player lose. to ensure the fairness of the game, the second player has a chance to move some (can be zero) heaps before the game starts, but he has to move one heap entirely, I. e. not partially. of course, he can't move all heaps out, at L East one heap shoshould be left for playing. Will the second player have the chance to win this time?
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the number of heaps. Then n integer AI follows, indicating the number of each heap.
[Technical Specification]
1. 1 <= T <= 100
2. 1 <= n <= 1 000
3. 1 <= AI <= 1 000 000 000 000
Output
For each test case, output "yes" if the second player can win by moving some (can be zero) heaps out, otherwise "no ".
Sample Input
3
1
2
3
2 2 2
5
1 2 3 4 5
Sample output
No
Yes
Yes

Question:

Nim revenge. With n piles of stones, the Houshou can take any heap of stones before starting the game and then play the NIM game, asking whether the Houshou has a chance to win.

Objective:

Let's first review the NIM game.

Given n heap stones, suppose I heap stones have a [I] stone heads. Two people take multiple items from a pile in turn, and each time at least one item is required, there are no limits on the number of winners.

Conventional solution for Nim games: K = A [1] ^ A [2] ^ A [3] ^... ^ A [n]

If K = 0, you must first enter

If K! = 0

Obviously, the goal of this question is to find a subset of the stone heap set (the number of subset elements is greater than or equal to 2), so that its elements are exclusive or followed by 0.

Solution:

Incorrect ideas:

During the competition, I tried to use the randomization algorithm to bring the water to the ground. I made a loop of times and submitted it three times .. If n is found to be less than 1000, the randomization algorithm cannot pass ..

Correct thinking:

Daniel's blog is not average. One or two lines of thought, after a whole day of research, I barely understood it. (There are three words for a big question: bitwise operations .. Orz)

According to the official solution, we need to regard these numbers as a binary matrix.

This matrix has the following properties:

(1) Every null value is 1 or 0.

(2) compared to a matrix element with only one or zero values, the exclusive or operation is equivalent to addition and subtraction: 1 + 1 = 0 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1-1 = 0 1-0 = 1 0-1 = 1 0-0 = 0

Based on the correlation theorem of rank and maximum independent groups of the matrix, we can see that if rank of the matrix is less than N, the number of members of the maximum independent group must be less than N, therefore, any A [I] (especially for a [I] that is not in a very large irrelevant group) can be expressed by a member in a very large irrelevant group.

According to the nature: For any number of a, a ^ A = 0, we can know that when and only when rank <n, there is a subset to make it exclusive or the value is 0.

Solution:

It is said that it is Gaussian deyuan. If the matrix is converted into a trapezoid, empty rows exist, that is, rank <n.

PS: As mentioned above, an exclusive or binary is equivalent to addition or subtraction. There is no conflict with the primary changes of the matrix.

Code:

1 /************************************** * *********************************** 2> File Name: bestcode # 16_1003.cpp 3> author: enumz 4> mail: [email protected] 5> created time: 6 ******************************* **************************************** */7 8 # include <iostream> 9 # include <cstdio> 10 # include <cstdlib> 11 # include <string> 12 # include <cstring> 13 # include <list> 14 # include <queue> 15 # include <stack> 16 # include <map> 17 # include <set> 18 # include <algorithm> 19 # include <cmath> 20 # include <bitset> 21 # include <time. h> 22 # include <climits> 23 # define maxn 300024 using namespace STD; 25 long a [maxn]; 26 int main () 27 {28 int T; 29 CIN> T; 30 While (t --) 31 {32 int N; 33 CIN> N; 34 for (INT I = 1; I <= N; I ++) 35 scanf ("% i64d", & A [I]); 36 int ROW = 1, Col = 1; 37 (; row <= N & Col <= 40; Col ++, row ++) // data range. After being converted to binary, the number of Columns cannot exceed 4038 {39 int tmp_row; 40 for (tmp_row = row; tmp_row <= N; tmp_row ++) // locate the first non-zero value in the current column. 41 if (a [tmp_row] & (1ll <col) // determine whether the value is 142 break; 43 If (tmp_row = n + 1) // if there is no point whose value is 1, there is an empty row, rank -- 44 {45 row --; 46 continue; 47} 48 swap (A [tmp_row], a [row]); // exchange row 49 for (INT I = tmp_row + 1; I <= N; I ++) 50 if (a [I] & (1ll <col) 51 A [I] ^ = A [row]; 52} 53 If (row <n) 54 printf ("Yes \ n"); 55 else56 printf ("NO \ n"); 57} 58 return 0; 59}

 

    

Hdu5088 -- revenge of Nim II (Gaussian elimination element & Matrix Rank) (bestcoder round #16)

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.