参考:https://blog.nowcoder.net/n/7cc0194417e741dab76023f4596046a7
思路:这题主要是要想到奇数的二进制位最后一个数是1,那么或操作要为奇数,只需要区间中有一个奇数即可,还有就是要考虑怎么做不会重复区间。因此,我们记录一个last_odd为上一个奇数位置
在遍历时,如果当前数为奇数,那么索引从1开始,[1, i]为左端点的区间都满足条件,直接ans += i即可;如果当前数为偶数,那么从索引1开始,[1, last_odd]为左端点的区间都满足条件,直接ans += last_odd即可,并且不会重复。最终,输出累加后的答案即可
代码:
import sys
input = lambda: sys.stdin.readline().strip()
import math
inf = 10 ** 18
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LI():
return input().split()
def LII():
return list(map(int, input().split()))
def LFI():
return list(map(float, input().split()))
fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))
'''
'''
def solve():
n = II()
a = LII()
ans = 0
last_odd = -1
for i, x in enumerate(a, 1):
if x & 1:
last_odd = i
ans += i
elif last_odd != -1:
ans += last_odd
print(ans)
t = 1
# t = II()
for _ in range(t):
solve()

京公网安备 11010502036488号