# GRIDXOR - Editorial

Author: Vishesh Saraswat
Tester: Istvan Nagy
Editorialist: Harshikaa Agrawal

Simple

# Prerequisites:

Bitwise operations

# Problem:

Given an integer N, print an N \times N matrix such that the bitwise XOR of each row, column and diagonal is the same.

# Explanation:

The bitwise XOR for each row, column and diagonal would be the same if all elements of the N \times N matrix are the same.

Given that 0\oplus0 is 0 and 1\oplus1 is 0, when N is even the value would be 0 if all values are the same.

Example: N = 4
We choose 5 as the number we output.

When N is odd, it means that N-1 is even and thus, the value for (N-1)+1 times is the number itself.

Example: N = 5
We choose 5 as the number we output.

Thus, output the same number (which is â‰¤ 10^9) in the entire N \times N grid.

# Time Complexity:

O(N^2) for each test case

# Setterâ€™s Solution

Setter's Solution
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()

using ll = long long;
const int mod = 1e9+7;

void solve(int tc) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << 1;
if (j < n-1)
cout << " ";
}
cout << '\n';
}
}

signed main() {
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
return 0;
}


# Testerâ€™s Solution

Tester's Solution
#include <iostream>
#include <algorithm>
#include <string>
#include <assert.h>
using namespace std;

long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
assert(cnt>0);
if(is_neg){
x= -x;
}
assert(l<=x && x<=r);
return x;
} else {
assert(false);
}
}
}

string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}

int main(){
for(int tc = 0; tc < T; ++tc)
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
printf("1 ");
}
printf("\n");
}
}
assert(getchar()==-1);
}


# Editorialistâ€™s solution:

Editorialist's Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout<<1<<" ";
}
cout<<"\n";
}
}
return 0;
}

4 Likes