You are given a set S and Q queries. Initially, S is empty. In each query:
You are given a positive integer X.
You should insert X into S.
For each y∈S before this query such that y≠X, you should also insert y⊕X into S (⊕ denotes the XOR operation).
Then, you should find two values E and O: the number of elements of S with an even number of 1's and with an odd number of 1's in the binary representation, respectively.
我尝试过将问题分解成更小的子问题,但由于输入大小和列表大小都很大,这似乎超出了时间。任何对代码的建议和进一步的优化都将非常有帮助。我已经使用了set,但是预期的输出与我的输出不一样。有没有关于我在解决方案代码中出错的地方的建议。。?在s=set()
def fun(n,q):
c=0
cc=0
s.add(n)
for k in range(len(list(s))):
if list(s)[k]!=n:
s.add((list(s)[k])^n)
for k in s:
if bin(k)[2::].count('1')%2==0:
c+=1
else:
cc+=1
print(c,cc)
for _ in range(int(input())):
q=int(input())
for j in range(q):
n=int(input())
fun(n,q)
输入示例:
^{pr2}$
输出示例:0 1
1 2
3 4