Grid Xor
Note: The XOR-sum of set?{s1,s2,…,sm}{s1,s2,…,sm}?is defined as?s1⊕s2⊕…⊕sms1⊕s2⊕…⊕sm, where?⊕⊕?denotes the?bitwise XOR operation.
After almost winning IOI, Victor bought himself an?n×nn×n?grid containing integers in each cell.?nn?is an even integer.?The integer in the cell in the?ii-th row and?jj-th column is?ai,jai,j.
Sadly, Mihai stole the grid from Victor and told him he would return it with only one condition: Victor has to tell Mihai the XOR-sum of?all?the integers in the whole grid.
Victor doesn't remember all the elements of the grid, but he remembers some information about it: For each cell, Victor remembers the XOR-sum of all its neighboring cells.
Two cells are considered neighbors if they share an edge — in other words, for some integers?1≤i,j,k,l≤n1≤i,j,k,l≤n, the cell in the?ii-th row and?jj-th column is a neighbor of the cell in the?kk-th row and?ll-th column if?|i?k|=1|i?k|=1?and?j=lj=l, or if?i=ki=k?and?|j?l|=1|j?l|=1.
To get his grid back, Victor is asking you for your help. Can you use the information Victor remembers to find the XOR-sum of the whole grid?
It can be proven that the answer is unique.
Input
The first line of the input contains a single integer?tt?(1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single?even?integer?nn?(2≤n≤10002≤n≤1000) — the size of the grid.
Then follows?nn?lines, each containing?nn?integers. The?jj-th integer in the?ii-th of these lines represents the XOR-sum of the integers in all the neighbors of the cell in the?ii-th row and?jj-th column.
It is guaranteed that the sum of?nn?over all test cases doesn't exceed?10001000?and in the original grid?0≤ai,j≤230?10≤ai,j≤230?1.
Hack Format
To hack a solution, use the following format:
The first line should contain a single integer t (1≤t≤1001≤t≤100) — the number of test cases.
The first line of each test case should contain a single?even?integer?nn?(2≤n≤10002≤n≤1000) — the size of the grid.
Then?nn?lines should follow, each containing?nn?integers. The?jj-th integer in the?ii-th of these lines is?ai,jai,j?in Victor's?original?grid. The values in the grid should be integers in the range?[0,230?1][0,230?1]
The sum of?nn?over all test cases must not exceed?10001000.
Output
For each test case, output a single integer — the XOR-sum of the whole grid.
Example
input
Copy
3 2 1 5 5 1 4 1 14 8 9 3 1 5 9 4 13 11 1 1 15 4 11 4 2 4 1 6 3 7 3 10 15 9 4 2 12 7 15 1output
Copy
4 9 5Note
For the first test case, one possibility for Victor's original grid is:
| 11 | 33 |
| 22 | 44 |
For the second test case, one possibility for Victor's original grid is:
| 33 | 88 | 88 | 55 |
| 99 | 55 | 55 | 11 |
| 55 | 55 | 99 | 99 |
| 88 | 44 | 22 | 99 |
For the third test case, one possibility for Victor's original grid is:
| 44 | 33 | 22 | 11 |
| 11 | 22 | 33 | 44 |
| 55 | 66 | 77 | 88 |
| 88 | 99 | 99 | 11 |
思路:題中給出我們每個數相鄰的異或和,讓我們求全部的異或和。我們是不是只要將數組的每一個值都異或一遍不就是答案了。題中給出相鄰異或和,是不是已經替我們遍歷過了周圍的數了,那么將他給的相鄰異或和記錄就行了,我們只需要記錄哪個數用過哪個數沒用過,用過了就不用管了,已經將他的和記錄了,(用過的是當前這個數周圍的數),沒用過將他的值加入答案即可標記周圍的數用過了。但可能出現的情況是這個數被用了兩次(因為是通過周圍影響的),這個數異或兩次相當于又去掉了,所以這個點應當是沒用過的,之后再加上即可。
周圍點標記:這個題像《費解的開關》一樣,都是標記周圍的數(只不過費解的開關是將當前點也標記,這個題不用)所以將這個題也從第二行開始遍歷,看當前點上面的那個點是否用過,沒有的話,記錄當前點的(周圍的點)即可,“加入”該異或和即可。
#include <iostream> #include <algorithm> #include <cstring>using namespace std;const int N=1010; int a[N][N]; int use[N][N];int main() {int t;cin>>t;while(t--){int n;cin>>n;memset(use,0,sizeof use);int ans=0;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin>>a[i][j];}}for(int i=2;i<=n;i++){for(int j=1;j<=n;j++){if(use[i-1][j]==0){ans^=a[i][j];use[i-1][j]^=1;use[i+1][j]^=1;use[i][j-1]^=1;use[i][j+1]^=1;}}}cout<<ans<<endl;}return 0; }總結
- 上一篇: XOR运算
- 下一篇: 异或(xor)的讲解和使用方法