Problem solution · Python

ARC144 A — Digit Sum of 2x

ARC144 A — Digit Sum of 2x: 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
38 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 ARC144 A — Digit Sum of 2x, 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

  • 38 lines of Python from the credited upstream file arc144_a.py.
  • The implementation keeps its working state in language-native values and containers.
  • 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 codeARC144 A — Digit Sum of 2x · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     n = int(input())     # 桁和と繰り上がり回数kの関係    # f(a + b) = f(a) + f(b) - 9k    # 繰り上がりが発生するとある桁が-10、一つ上の桁が+1になるため     # m:    # f(2x) = f(x + x) = f(x) + f(x) - 9k = 2n - 9k    # Mを最大化するには、-9kが0だと嬉しい    # 桁の繰り上がりが発生しないのは、各桁とも4以下の場合    # f(2x) = 2n = m    m = 2 * n     # x:    # できるだけ桁数を少なく = 利用できる数のうち最大の値を使うと良さそう    # 基本的には4で埋める    # mod 4で余りが0以外のときは、先頭にその余りをつけると良さそう    p, q = divmod(n, 4)    x = '4' * p     if q != 0:        x = str(q) + x        print(m)    print(x)  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 ↗