Cerinta completa

An array, , is defined as follows:

  • for , where is the symbol for XOR

You will be given a left and right index . You must determine the XOR sum of the segment of as .

For example, . The segment from to sums to .

Print the answer to each question.

Function Description

Complete the xorSequence function in the editor below. It should return the integer value calculated.

xorSequence has the following parameter(s):

  • l: the lower index of the range to sum
  • r: the higher index of the range to sum

Input Format

The first line contains an integer , the number of questions.
Each of the next lines contains two space-separated integers, and , the inclusive left and right indexes of the segment to query.

Constraints


Output Format

On a new line for each test case, print the XOR-Sum of ‘s elements in the inclusive range between indices and .

Sample Input 0

3
2 4
2 8
5 9

Sample Output 0

7
9
15

Explanation 0

The beginning of our array looks like this:

Test Case 0:

Test Case 1:

Test Case 2:

Sample Input 1

3
3 5
4 6
15 20

Sample Output 1

5
2
22

Explanation 1

. Perform the xor sum on each interval:



Limbajul de programare folosit: python3

Cod:

#!/bin/python3

import sys

def sequence(N):
    x = N % 8
    if x == 0 or x == 1:
        return N
    if x == 2 or x == 3:
        return 2
    if x == 4 or x == 5:
        return N+2
    if x == 6 or x == 7:
        return 0

def solution(L, R):
    res = 0
    if (R - L) % 2 == 1:
        for ind in range(L+1, R+1, 2):
            res ^= ind
    else:
        for ind in range(L+1):
            res ^= ind
        for ind in range(L+2, R+1, 2):
            res ^= ind
            
    return res
        

Q = int(input().strip())
for a0 in range(Q):
    L,R = input().strip().split(' ')
    L,R = [int(L),int(R)]
    #print(solution(L,R))
    print(sequence(L-1)^sequence(R))

Scor obtinut: 1.0

Submission ID: 464605077

Link challenge: https://www.hackerrank.com/challenges/xor-se/problem

Xor-sequence