Problem solution · Python

ABC365 E — Xor Sigma Problem

ABC365 E — Xor Sigma Problem: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
39 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC365 E — Xor Sigma Problem, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 39 lines of Python from the credited upstream file abc365_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC365 E — Xor Sigma Problem · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys    from itertools import accumulate    from operator import xor     input = sys.stdin.readline     n = int(input())    a = list(map(int, input().split()))    s = list(accumulate(a, xor, initial=0))    ans = 0     # 累積和の考え方をベース、累積xorを取る    # 差分は、s[r] xor s[l - 1]     # bit演算では、桁ごとに独立して考えるのが定石    # 合計に寄与するのは、xorが1となる場合のみ(片方が0で、もう片方が1のとき)    # 各桁について、1となる個数 * (n + 1 - 1となる個数) * (2 ** k)の総和    # ただし、区間の長さが1の場合を含んでいるため、最後に引く    for digit in range(30):        one_count = 0         for si in s:            if si >> digit & 1:                one_count += 1         ans += one_count * (n + 1 - one_count) * (2**digit)     ans -= sum(a)     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗