日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Grid Xor

發布時間:2023/12/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 1

output

Copy

4 9 5

Note

For the first test case, one possibility for Victor's original grid is:

1133
2244

For the second test case, one possibility for Victor's original grid is:

33888855
99555511
55559999
88442299

For the third test case, one possibility for Victor's original grid is:

44332211
11223344
55667788
88999911

思路:題中給出我們每個數相鄰的異或和,讓我們求全部的異或和。我們是不是只要將數組的每一個值都異或一遍不就是答案了。題中給出相鄰異或和,是不是已經替我們遍歷過了周圍的數了,那么將他給的相鄰異或和記錄就行了,我們只需要記錄哪個數用過哪個數沒用過,用過了就不用管了,已經將他的和記錄了,(用過的是當前這個數周圍的數),沒用過將他的值加入答案即可標記周圍的數用過了。但可能出現的情況是這個數被用了兩次(因為是通過周圍影響的),這個數異或兩次相當于又去掉了,所以這個點應當是沒用過的,之后再加上即可。

周圍點標記:這個題像《費解的開關》一樣,都是標記周圍的數(只不過費解的開關是將當前點也標記,這個題不用)所以將這個題也從第二行開始遍歷,看當前點上面的那個點是否用過,沒有的話,記錄當前點的(周圍的點)即可,“加入”該異或和即可。

#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; }

總結

以上是生活随笔為你收集整理的Grid Xor的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。