-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathSansa and XOR.py
More file actions
49 lines (39 loc) · 1.28 KB
/
Sansa and XOR.py
File metadata and controls
49 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Problem Statement
Sansa has an array. She wants to find the value obtained by XOR-ing the contiguous subarrays, followed by XOR-ing the
values thus obtained. Can you help her in this task?
Note : [1,2,3] is contiguous subarray of [1,2,3,4] while [1,2,4] is not.
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
consider array 1, 2, 3, 4, 5, 6, 7
consider 3, number of appearance time in consecutive subset: 1+2+3+3+3+2+1 = 3*(7-(3-1))
consider 4: 1+2+3+4+3+2+1 = 4*(7-(4-1))
consider 6: 1+2+2+2+2+2+1 = (7-(6-1))*6
consider i: 1+2+..i+...+1 = i*(l-(i-1))
if start from 0:
(i+1)*(N-i)
:param cipher: the cipher
"""
N, A = cipher
ret = 0
for i, val in enumerate(A):
if (i + 1) * (N - i) % 2 == 1:
ret ^= val
return ret
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
solution = Solution()
testcases = int(f.readline().strip())
for t in xrange(testcases):
# construct cipher
N = int(f.readline().strip())
A = map(int, f.readline().strip().split(' '))
cipher = N, A
# solve
s = "%s\n" % (solution.solve(cipher))
print s,