ABC295D - Three Days Ago

解法1:

考え方

各数字(0〜9)の出現回数偶奇性が$l -1$と$r$で一致する場合に「嬉しい列」となる.
よって,bitで$i$文字目までの0〜9の出現回数の偶奇性だけを抑えておく.

回答例

$l - 1 = 0$の場合をd[0] = 1として考える.

from collections import defaultdict

S = list(input())
d = defaultdict(int)
bit = 0
d[0] = 1
for s in S:
    s = int(s)
    bit ^= (1 << s)
    d[bit] += 1

ans = 0
for key in d:
    n = d[key]
    ans += n * (n - 1) // 2

print(ans)