Matrix XOR CodeChef Solution

Problem -Matrix XOR CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Matrix XOR CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define endl "\n"
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);

int32_t main()
{
    fio;
    int t;
    cin >> t;
    while (t > 0)
    {
        int n, m, k;
        cin >> n >> m >> k;

        // We will not actually create that matrix;
        int sum = 0;
        for (int j = 1; j <= m; j++) // first row diagonals;
        {
            if (min(j, n) % 2 == 0)
                sum = (sum ^ 0);
            else
                sum = (sum ^ (k + 1 + j));
        }
        for (int i = 2; i <= n; i++) // last column diagonals;
        {
            if (min(n - i + 1, m) % 2 == 0)
                sum = (sum ^ 0);
            else
                sum = (sum ^ (k + i + (m)));
        }
        cout << sum << endl;
        t--;
    }
    return 0;
}
/*
After creating that matrix :
k+1+1 k+1+2 ...........k+1+m
k+2+1 k+2+2............k+2+m
k+3+1..................k+3+m
............................
............................
k+n+1 .................k+n+m

Clearly some values will get cancelled on doing XOR operation;
Clearly all right-side tilted diagonals having same values;
all those diagonals are starting from either any element of 1st row or last column;

Any element on first row
        (i,j)=(1,j) =====> how many elements (including this one as well) will be present on this diagonal (feel this diagonal is going south-west);
                  ======> min(no. of elements on left side,no. of elements on down side) = min(j,N);
Any element on last column
        (i,j)=(i,M) =====> how many elements (including this one as well) will be present on this diagonal (feel this diagonal is also going south-west);
                  ======> min(no. of elements on left side,no. of elements on down side) = min(j,n-i+1);
*/

Matrix XOR CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define bpc __builtin_popcount
#define forn(i,e) for(ll i = 0; i < e; i++)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define per(i,a,b) for(ll i=a;i>=b;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define fi                first
#define se                second
#define sz(x)             (int)((x).size())
#define all(x)            (x).begin(),(x).end()
#define alr(s)            s.rbegin(),s.rend()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define vi vector<ll>
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>
#define pi pair<ll,ll>
#define vppi vector<pair<pair<ll,ll>,ll>>
#define pq_max priority_queue<ll>
#define pqp_max priority_queue<pi>
#define pq_min priority_queue<ll, vector<ll>, greater<ll>>
#define pqp_min priority_queue<pi, vector<pi>, greater<pi>>
#define m_pi              3.141592653589793238
#define lb lower_bound
#define ub upper_bound
#define uset unordered_set<ll>
#define oset set<ll>
#define umap unordered_map<ll,ll>
#define omap map<ll,ll>
void solve()
{
   ll m,n,k;
   cin>>m>>n>>k;
   ll mx=max(m,n);
   ll mn=min(m,n);
   ll ans=0;
   ll i;
   for(i=1;i<=mx;i++)
   {
       ll z=min(i,mn);
       if((z)%2!=0)
       {
           ans=(ans^(i+1+k));
           //cout<<i<<" ";
       }
   }
   ll j;
   ll f=0;
   if((mn-1)&1)f=1;
   for(j=i;j<=m+n-1;j++)
   { //cout<<j<<" ";
       if(f==1)
       {
           ans=(ans^(j+1+k));
          // cout<<j<<" ";
       }
       f=1-f;
   }
   cout<<ans<<endl;
    
}
int main() {
	// your code goes here
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
	    solve();
	    
	}
	return 0;
}

Matrix XOR CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
for i in range(int(input())):
    a,b,c=map(int,input().split())
    e=0
    for i in range(2,a+2):
        d=min(i-1,b)
        if (d)%2==0:
            pass
        else:
            e^=(i+c)
    for i in range(a+2,a+b+1):
        f=min(a,b-(i-a)+1)
        if f%2==0:
            pass
        else:
            e^=(i+c)
    print(e)

Matrix XOR CodeChef Solution in C

#include <stdio.h>
#define min(a,b) (((a)<(b))? (a):(b))
int main(void) {
	int t;
	scanf("%d",&t);
	while(t!=0)
	{
	     int n,m,k,i,j,a=0;
	     scanf("%d %d %d",&n,&m,&k);
	     
	     for(i=1;i<n+1;i++)
	     {
	          if(min(i,m)%2==1)
	          a^=k+i+1;
	     }
	     for(j=2;j<m+1;j++)
	     {
	          if(min(n,m-j+1)%2==1)
	          a^=k+n+j;
	     }
	     
	     printf("%d\n",a);
	     
	     t--;
	}
	return 0;
}

Matrix XOR CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer st = new StringTokenizer(br.readLine());
    int T = Integer.parseInt(st.nextToken());
    for (int tc = 0; tc < T; ++tc) {
      st = new StringTokenizer(br.readLine());
      int N = Integer.parseInt(st.nextToken());
      int M = Integer.parseInt(st.nextToken());
      int K = Integer.parseInt(st.nextToken());

      System.out.println(solve(N, M, K));
    }
  }

  static int solve(int N, int M, int K) {
    int result = 0;
    for (int i = 2; i <= N + M; ++i) {
      int minR = Math.max(1, i - M);
      int maxR = Math.min(N, i - 1);

      if ((maxR - minR + 1) % 2 != 0) {
        result ^= i + K;
      }
    }

    return result;
  }
}

Matrix XOR CodeChef Solution in PYPY 3

import math


def findXOR(n):
    # N & 3 is equivalent to n % 4
    if (n % 4 == 0):

        # If n is multiple of 4
        return n
    elif n % 4 == 1:

        # If n % 4 gives remainder 1
        return 1

    # If n % 4 gives remainder 2
    elif n % 4 == 2:
        return n + 1;

    # If n % 4 gives remainder 3
    elif (n % 4 == 3):
        return 0;


# Function to find the XOR of odd
# numbers less than or equal to N
def findOddXOR(n):
    # If number is even
    if (n % 2 == 0):

        # Prthe answer
        return (findXOR(n)) ^ (2 * findXOR(n // 2))

    # If number is odd
    else:

        # Prthe answer
        return (findXOR(n)) ^ (2 * findXOR((n - 1) // 2))


def get_xor_range(range, action):
    if action == 'EVEN':
        return 2 * (findXOR(range[1]// 2) ^ findXOR(range[0]//2 - 1))
    else:
        return findOddXOR(range[1]) ^ findOddXOR(range[0] - 1)


def get_mat_xor(n, m, k):

    if n %2 == 0:
        # print("n is even")
        if k % 2 == 0:
            left = get_xor_range([k + 2, k + n], 'EVEN')
        else:
            left = get_xor_range([k + 2, k + n], 'ODD')

        # print('left', left)

        # now getting the right part

        if (k + 2 + m) % 2 == 0:
            right = get_xor_range([k + 2 + m, k + n + m], 'EVEN')
        else:
            right = get_xor_range([k + 2 + m, k + n + m], 'ODD')

        # print('right', right)

        return left ^ right

    else:
        # print("n is odd")
        if k % 2 == 0:
            left = get_xor_range([k + 2, k + n - 1], 'EVEN')
        else:
            left = get_xor_range([k + 2, k + n - 1], 'ODD')

        # print('left', left)

        # now getting the right part

        if (k + 2 + m) % 2 == 0:
            right = get_xor_range([k + 2 + m, k + n - 1 + m], 'EVEN')
        else:
            right = get_xor_range([k + 2 + m, k + n - 1 + m], 'ODD')

        # print('right', right)

        return left ^ right ^ (findXOR(k + n + m) ^ (findXOR(k + n + 1 - 1)))


# if __name__ == '__main__':
#     print(get_xor_range([3, 5], 'ODD'))


if __name__ == '__main__':

    for t in range(int(input())):
        n, m, k = map(int, input().split())
        print(get_mat_xor(n, m, k))

        

Matrix XOR CodeChef Solution in PYTH

t = int(raw_input())
for i in range(t):
	st = raw_input().split()
	N = int(st[0])
	M = int(st[1])
	K = int(st[2])
	if M > N:
		M,N = N,M
	# endif
	V1 = 2+K
	V2 = M+1+K
	V3 = M+2+K
	V4 = N+K
	V5 = N+1+K
	if N == M:
		V5 = N+2+K
	# endif
	V6 = N+M+K
	r = 0
	if M%2 == 1:
		for n in range(V3,V4+1):
			r = r^n
		# endfor n
	# endif
	if K%2 == 0:
		for n in range(V1,V2+1):
			if n%2 == 0:
				r = r^n
			# endif
		# endfor n
		if (N+M)%2 == 0:
			for n in range(V5,V6+1):
				if n%2 == 0:
					r = r^n
				# endif
			# endfor n
		else:
			for n in range(V5,V6+1):
				if n%2 == 1:
					r = r^n
				# endif
			# endfor n
		# endif
	else:
		for n in range(V1,V2+1):
			if n%2 == 1:
				r = r^n
			# endif
		# endfor n
		if (N+M)%2 == 0:
			for n in range(V5,V6+1):
				if n%2 == 1:
					r = r^n
				# endif
			# endfor n
		else:
			for n in range(V5,V6+1):
				if n%2 == 0:
					r = r^n
				# endif
			# endfor n
		# endif
	# endif
	print r
# endfor i

Matrix XOR CodeChef Solution in C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

public class Test
{
	public static void Main()
	{
	
            MatrixXOR.Run();
	}
	
	 public class MatrixXOR
    {
        public static void Run()
        {
            var T = Convert.ToInt32(Console.ReadLine());
            var resul = new StringBuilder();
            for (int t = 0; t < T; t++)
            {                
                  var A = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), long.Parse);

                var N = A[0];
                var M = A[1];
                var K = A[2];              

                long res2 = 0;
                for (int i = 2; i <= N + M; i++)
                {
                    long countMax = i - 1;
                    long count = countMax;
                    if (N < countMax)
                        count = countMax - (Math.Abs(N - countMax));

                    if (M < countMax)
                        count = count - (Math.Abs(M - countMax));

                    if (count % 2 != 0)
                    {
                        res2 = res2 ^ (i + K);
                    }
                }
              

                resul.AppendLine(res2 + "");
            }

            Console.WriteLine(resul.ToString().Trim());
        }
    }
}

Matrix XOR CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {

	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		m, n, k := readThreeNums(reader)
		res := solve(k, m, n)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func solve(k int, m int, n int) int {
	// k + 1 + 1 ..... k + m + n
	// 那么在d为的1的数量要怎么计算呢?
	// k <= 10e9
	// m, n <= 10e6

	cnt := make([]int, n+m+2)

	for i := 1; i <= n; i++ {
		l := i + 1
		r := i + m
		cnt[l]++
		cnt[r+1]--
	}

	var res int
	for i := 2; i <= n+m; i++ {
		cnt[i] += cnt[i-1]
		if cnt[i]&1 == 1 {
			res ^= (k + i)
		}
	}
	return res
}

func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
Matrix XOR CodeChef Solution Review:

In our experience, we suggest you solve this Matrix XOR CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Matrix XOR CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Matrix XOR CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *