Problem solution · Python

ABC368 F — Dividing Game

ABC368 F — Dividing Game: 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
55 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 ABC368 F — Dividing Game, 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

  • 55 lines of Python from the credited upstream file abc368_f.py.
  • The implementation visibly relies on sequence storage, hash lookup, 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 codeABC368 F — Dividing Game · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def run_prime_factorization(max_number: int) -> dict:    from math import sqrt     ans = dict()    remain = max_number     for base in range(2, int(sqrt(max_number)) + 1):        if remain % base == 0:            exponent_count = 0             while remain % base == 0:                exponent_count += 1                remain //= base             ans[base] = exponent_count     if remain != 1:        ans[remain] = 1     return ans  def main():    import sys     input = sys.stdin.readline     n = int(input())    a = list(map(int, input().split()))    b = []     # Nimによる勝敗判定の問題に帰着    for ai in a:        ps = run_prime_factorization(ai)        count = sum(ps.values())        b.append(count)     # print(b)    result = 0     for bi in b:        result ^= bi     if result != 0:        print("Anna")    else:        print("Bruno")  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 ↗